Explore the Experience in SharePoint 2013

Thursday, December 4, 2014

How to get User data from Active Directory in SharePoint in C#

No comments


Hi Guys,

Thanks for visiting my blog .

In this Post , Am going to get User data from Active Directory in C# .We can also use the code in SharePoint also as per the requirement.

Please find the code for getting user details from AD (Active directory ).

Before writing the code We need to get System.DirectoryServices dll from assembiles. ino the page.

Best Practice : Get the Server Name from Web.config file .

         protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string connection = "LDAP:// server name"; // LDAP Server Name
                DirectorySearcher dssearch = new DirectorySearcher(connection);
                dssearch.Filter = "(sAMAccountName=" + txtusername.Text + ")"; // Passing UserName
                SearchResult sresult = dssearch.FindOne();
                DirectoryEntry dsresult = sresult.GetDirectoryEntry(); // retrieving data
                string FirstName = dsresult.Properties["givenName"][0].ToString();
                string LastName = dsresult.Properties["sn"][0].ToString();
                string EmailID = dsresult.Properties["mail"][0].ToString();
            }
            catch (Exception ex)
            {
               
             
            }
        }

No comments :

Post a Comment