Friday, March 23, 2012

How to retrieve user data from Active Directory in SharePoint 2010 using C#

In SharePoint logged in user can click on chat button to communicate with IT Help desk team.
In this requirement we need to pass users login name,IP Address, Country and City of the user to the Help desk team. The team using the Provide Support Chat tool.

Have developed a web part to deploy in SharePoint 2010.

User name and IP Addresses are coming from the below statements in the user control.
lblUserName.Text= Request.ServerVariables["LOGON_USER"];
lblIP.Text = Request.ServerVariables["REMOTE_ADDR"];
The Country ,City and other properties coming from AD
using System.DirectoryServices;
findBase.Add("LDAP://XXX/OU=Users,OU=Bangalore-CRL,OU=Pacific Asia,DC=corp,DC=lairdtech,DC=com");
findBase.Add("LDAP://XXX/OU=Users,OU=Sharepoint Users,DC=corp,DC=lairdtech,DC=com");
(Collection of LDAPs)
foreach (string str in findBase)
DirectoryEntry myLdapConnection = createDirectoryEntry(str);
// create search object which operates on LDAP connection object
// and set search object to only find the user specified
DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = "(cn=" + userName + ")";
SearchResult result = search.FindOne();
if (result != null)
if (result.Properties["c"].Count > 0)
if (result.Properties["l"].Count > 0)
// else Console.WriteLine("User not found!" +i.ToString());
return userDetails;
public DirectoryEntry createDirectoryEntry(string findBase)
// create and return new LDAP connection with desired settings
DirectoryEntry ldapConnection = new DirectoryEntry(findBase, "corp\\NA-Service", "0ZLXXXGKXXX");
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
return ldapConnection;
Next passing the user details to the chat tool

Once user clicks on the chat button then Help desk team receives the users details like the below screen shot

Finally have deployed the IT Help desk chat web part on the home page the sharepoint 2010.
Useful Links:

1 comment:

Trading said...

Hello Friends,

This blog shows the information about How to retrieve user data from Active Directory in SharePoint 2010 using C#. Thanks for providing valuable information......
Active Directory Group Membership