locked
Get Details of Single User from Active Directory RRS feed

  • Question

  • Hi..

    How to get User Information of a Single User from Active Directory using Principal Context Class in SharePoint 2010

     


    Ravindranath

    Monday, June 3, 2013 6:44 AM

Answers

  • Hi Ravindranath

    Using below code you can get User Information detail from AD

    using System.DirectoryServices.AccountManagement;
    using System.DirectoryServices;

    using (var domainContext = new PrincipalContext(ContextType.Domain, "YourDomain.com", "DC=YourDomain,DC=com"))
                        {
                                using (var foundUser = UserPrincipal.FindByIdentity(domainContext, System.DirectoryServices.AccountManagement.IdentityType.SamAccountName, "<UserLoginName>"))
                                {
                                    if (foundUser != null)
                                    {
                                        try
                                        {
                                            DirectoryEntry directoryEntry = foundUser.GetUnderlyingObject() as DirectoryEntry;
                                            string strdepartment = directoryEntry.Properties["department"].Value.ToString();
                                            string strcompany = directoryEntry.Properties["company"].Value.ToString();
                                         //many details
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine(ex.Message);
                                        }
                                    }
                                }
                            }


    Please mark the replies as answers if they help or unmark if not.

    • Marked as answer by Tagore 534 Wednesday, June 5, 2013 9:21 AM
    Monday, June 3, 2013 7:02 AM

All replies

  • Hi Ravindranath

    Using below code you can get User Information detail from AD

    using System.DirectoryServices.AccountManagement;
    using System.DirectoryServices;

    using (var domainContext = new PrincipalContext(ContextType.Domain, "YourDomain.com", "DC=YourDomain,DC=com"))
                        {
                                using (var foundUser = UserPrincipal.FindByIdentity(domainContext, System.DirectoryServices.AccountManagement.IdentityType.SamAccountName, "<UserLoginName>"))
                                {
                                    if (foundUser != null)
                                    {
                                        try
                                        {
                                            DirectoryEntry directoryEntry = foundUser.GetUnderlyingObject() as DirectoryEntry;
                                            string strdepartment = directoryEntry.Properties["department"].Value.ToString();
                                            string strcompany = directoryEntry.Properties["company"].Value.ToString();
                                         //many details
                                        }
                                        catch (Exception ex)
                                        {
                                            Console.WriteLine(ex.Message);
                                        }
                                    }
                                }
                            }


    Please mark the replies as answers if they help or unmark if not.

    • Marked as answer by Tagore 534 Wednesday, June 5, 2013 9:21 AM
    Monday, June 3, 2013 7:02 AM
  • Hi Ravindra,

    Try this: http://www.sharepointpills.com/2011/09/check-if-sharepoint-user-is-member-of.html

    protected void CurrentUserIsMemberOfGroup(string groupName)  
    {  
        string userLogin = SPContext.Current.Web.CurrentUser.LoginName;  
        // To get the right context, run with elevated privileges  
        SPSecurity.RunWithElevatedPrivileges(delegate()   
        {  
            var principalContext = new PrincipalContext(ContextType.Domain);  
            var userPrincipal = UserPrincipal.FindByIdentity(principalContext, System.DirectoryServices.AccountManagement.IdentityType.SamAccountName, userLogin);  
    	if (userPrincipal != null)
                                    {
                                            DirectoryEntry directoryEntry = userPrincipal.GetUnderlyingObject() as DirectoryEntry;
                                            string strdepartment = directoryEntry.Properties["department"].Value.ToString();
                                            string strcompany = directoryEntry.Properties["company"].Value.ToString();
                                         //You can pull required details from active directory.
                                    }
             
        });  
    }


    Please don't forget to 'mark answer/propose answer' or 'vote as helpful' as appropriate.


    Monday, June 3, 2013 7:05 AM
  • Please check whether below code help you or not:

    PrincipalContext PrincipalContext1 = new PrincipalContext(ContextType.Domain, stringDomainKey,"Any AD UserName to get to read ad details","Password of User");

    UserPrincipal UserPrincipal1 = new UserPrincipal(PrincipalContext1);

    PrincipalSearcher search = new PrincipalSearcher(UserPrincipal1);

    //UserName Name  of user you  want to search from AD.

    IEnumerable<System.DirectoryServices.AccountManagement.Principal> collsearch = search.FindAll().Where(a => a.SamAccountName != null && a.DisplayName != null).Where(a => a.SamAccountName.ToUpper().Contains(UserName.ToUpper()) || a.DisplayName.ToUpper().Contains(UserName.ToUpper()));

    foreach (UserPrincipal result in collsearch)

    {

    User User1 = new User( result.SamAccountName, result.DisplayName,

    result.Name, result.GivenName, result.Surname,

    result.Description, result.Enabled, result.LastLogon);

    }

    • Proposed as answer by Sameer Rocks Thursday, June 6, 2013 11:36 AM
    • Unproposed as answer by Sameer Rocks Thursday, June 6, 2013 11:36 AM
    • Proposed as answer by Sameer Rocks Thursday, June 6, 2013 11:36 AM
    • Unproposed as answer by Sameer Rocks Thursday, June 6, 2013 11:36 AM
    Monday, June 3, 2013 7:06 AM