locked
How to retrieve the entire employee information from Active Directory? RRS feed

  • Question

  • User-1651604128 posted

    Hi, I am looking a way to retrieve the entire employee information from active directory. these are the codes I used. But I can not find all data, I only get employee name or login id.

    The other data such Employee Number, Email address, Superviser name, Phone Number, etc can not be retrieved. I may use the wrong object.

    Can anybody show me what is the correct object for retrieving all employee information?

     

    var searcher = new DirectorySearcher();            

    searcher.PropertiesToLoad.Add("name");            

    searcher.PropertiesToLoad.Add("samaccountname");            

    searcher.PropertiesToLoad.Add("userprincipalname");            

    searcher.Filter = String.Format("(&(objectClass=user)(samaccountname={0}))", loginId);            

     var results = searcher.FindAll();

                return results.Cast<SearchResult>()                

    .Where(s => s.Properties.Contains("userprincipalname"))              

       .Select(TranslateSearchResultToADUser).Single();

    Tuesday, October 22, 2013 6:30 PM

Answers

  • User-1454326058 posted

    Hi Peter,

    Thanks for your post!

    The other data such Employee Number, Email address, Superviser name, Phone Number, etc can not be retrieved

    I think you should add the properities that you want into DirectorySearcher, like this code:

    searcher.PropertiesToLoad.AddRange(new string[] { "name", "Path", "displayname", "mail" });

    There is a simple sample (I searched) that may help you:

        private void button1_Click(object sender, EventArgs e)
            {
                ///   1.   objectClass=organizationalUnit   search all OU     
                ///   2.   objectClass=group   search all GROUP     
                ///   3.   objectClass=user   search all USER  
                string schemaClassNameToSearch = "organizationalUnit";
                
                string[] ADOU = GetOrganizationalUnit(txtAD.Text, txtUserName.Text, txtPwd.Text, schemaClassNameToSearch);
                schemaClassNameToSearch = "user";
                string[] ADUser = GetUserUnit(txtAD.Text, txtUserName.Text, txtPwd.Text, schemaClassNameToSearch);
    
                foreach (string s in ADOU)
                {
                    TreeNode node = new TreeNode(s);
                    tree.Nodes.Add(node);
    
                    AddUser(ADUser, node);
                }
            }
    
            private void AddUser(string[] ADUser, TreeNode node)
            {
                if (ADUser.Length <= 0)
                    throw new Exception("There aren't any user!");
                if (node == null)
                    throw new ArgumentNullException("node");
                foreach (string s in ADUser)
                {
                    string[] sSplit = s.Split(',');
                    if (sSplit.Length > 0)
                    {
                        if (sSplit[0] == node.Text)
                        {
                            TreeNode childrenNode = new TreeNode(sSplit[2]);
                            node.Nodes.Add(childrenNode);
                        }
                    }
                }
            }
    
            private string[] GetUserUnit(string domainADsPath, string username, string password, string schemaClassNameToSearch)
            {
                SearchResultCollection results = _ADHelper(domainADsPath, username, password, schemaClassNameToSearch);
                string[] sRe = GetGetUserUnitResults(results);
    
                results.Dispose();
    
                return sRe;
            }
    
            private string[] GetGetUserUnitResults(SearchResultCollection results)
            {
                string sRe = string.Empty;
                if (results.Count == 0)
                    throw new Exception("There aren't any user!");
                else
                {
                    foreach (SearchResult result in results)
                    {
                        string adPath = result.Path;
                        if (adPath.IndexOf("OU=User") < 0)
                            continue;
                       
                        string[] sSplit = adPath.Split(',');
                        string orz = string.Empty;
                        if (sSplit.Length > 2)
                        {
                            orz = sSplit[1].Split('=')[1];
                        }
                        ResultPropertyCollection propColl = result.Properties;
                        if (propColl["samaccountname"].Count > 0)
                            orz += "," + propColl["samaccountname"][0].ToString();
                        if (propColl["name"].Count > 0)
                            orz += "," + propColl["name"][0].ToString();
                        if(propColl["mail"].Count > 0)
                            orz += "," + propColl["mail"][0].ToString();
                        sRe += orz + "=";
                    }
                }
                if (sRe.Length > 0)
                    sRe = sRe.Substring(0, sRe.Length - 1);
                return sRe.Split('=');
            }
    
            
    
            private string[] GetOrganizationalUnit(string domainADsPath, string username, string password,string schemaClassNameToSearch)
            {
                SearchResultCollection results = _ADHelper(domainADsPath, username, password, schemaClassNameToSearch);
                string[] sRe = GetGetOrganizationalUnitResults(results);
    
                results.Dispose();
    
                return sRe;
            }
    
            private static SearchResultCollection _ADHelper(string domainADsPath, string username, string password, string schemaClassNameToSearch)
            {
                DirectorySearcher searcher = new DirectorySearcher();
    
                searcher.SearchRoot = new DirectoryEntry(domainADsPath,
                                                            username, password);
                searcher.Filter = "(objectClass=" + schemaClassNameToSearch + ")";
    
                searcher.SearchScope = SearchScope.Subtree;
                searcher.Sort = new SortOption("name",
                                                    SortDirection.Ascending);
                searcher.PageSize = 512;
    
                searcher.PropertiesToLoad.AddRange(new string[] { "name", "Path", "displayname", "samaccountname", "mail" });
    
                SearchResultCollection results = searcher.FindAll();
                return results;
            }
    
            private string[] GetGetOrganizationalUnitResults(SearchResultCollection results)
            {
                string sRe = string.Empty;
                if (results.Count == 0)
                    throw new Exception("There aren't any ou");
                else
                {
                    foreach (SearchResult result in results)
                    {
                        if (result.Path.IndexOf("OU=User") < 0)
                            continue;
                        ResultPropertyCollection propColl = result.Properties;
                        sRe += propColl["name"][0].ToString() + ',';
                    }
                }
                if (sRe.Length > 0)
                    sRe = sRe.Substring(0, sRe.Length - 1);
                return sRe.Split(',');
            }

    Thanks

    Best Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 24, 2013 1:37 AM

All replies

  • User178204871 posted

    To load all the attributes, you should not configure the field PropertiesToLoad. In your above case, searcher will load only name, samaccountname and userprincipalname attributes only.

    Wednesday, October 23, 2013 12:38 AM
  • User-1651604128 posted

    To load all the attributes, you should not configure the field PropertiesToLoad. In your above case, searcher will load only name, samaccountname and userprincipalname attributes only.

    Hi ASNALbert, thanks a lot for your help, could you please post any examples, so I can follow. thanks

    Wednesday, October 23, 2013 7:40 AM
  • User-1454326058 posted

    Hi Peter,

    Thanks for your post!

    The other data such Employee Number, Email address, Superviser name, Phone Number, etc can not be retrieved

    I think you should add the properities that you want into DirectorySearcher, like this code:

    searcher.PropertiesToLoad.AddRange(new string[] { "name", "Path", "displayname", "mail" });

    There is a simple sample (I searched) that may help you:

        private void button1_Click(object sender, EventArgs e)
            {
                ///   1.   objectClass=organizationalUnit   search all OU     
                ///   2.   objectClass=group   search all GROUP     
                ///   3.   objectClass=user   search all USER  
                string schemaClassNameToSearch = "organizationalUnit";
                
                string[] ADOU = GetOrganizationalUnit(txtAD.Text, txtUserName.Text, txtPwd.Text, schemaClassNameToSearch);
                schemaClassNameToSearch = "user";
                string[] ADUser = GetUserUnit(txtAD.Text, txtUserName.Text, txtPwd.Text, schemaClassNameToSearch);
    
                foreach (string s in ADOU)
                {
                    TreeNode node = new TreeNode(s);
                    tree.Nodes.Add(node);
    
                    AddUser(ADUser, node);
                }
            }
    
            private void AddUser(string[] ADUser, TreeNode node)
            {
                if (ADUser.Length <= 0)
                    throw new Exception("There aren't any user!");
                if (node == null)
                    throw new ArgumentNullException("node");
                foreach (string s in ADUser)
                {
                    string[] sSplit = s.Split(',');
                    if (sSplit.Length > 0)
                    {
                        if (sSplit[0] == node.Text)
                        {
                            TreeNode childrenNode = new TreeNode(sSplit[2]);
                            node.Nodes.Add(childrenNode);
                        }
                    }
                }
            }
    
            private string[] GetUserUnit(string domainADsPath, string username, string password, string schemaClassNameToSearch)
            {
                SearchResultCollection results = _ADHelper(domainADsPath, username, password, schemaClassNameToSearch);
                string[] sRe = GetGetUserUnitResults(results);
    
                results.Dispose();
    
                return sRe;
            }
    
            private string[] GetGetUserUnitResults(SearchResultCollection results)
            {
                string sRe = string.Empty;
                if (results.Count == 0)
                    throw new Exception("There aren't any user!");
                else
                {
                    foreach (SearchResult result in results)
                    {
                        string adPath = result.Path;
                        if (adPath.IndexOf("OU=User") < 0)
                            continue;
                       
                        string[] sSplit = adPath.Split(',');
                        string orz = string.Empty;
                        if (sSplit.Length > 2)
                        {
                            orz = sSplit[1].Split('=')[1];
                        }
                        ResultPropertyCollection propColl = result.Properties;
                        if (propColl["samaccountname"].Count > 0)
                            orz += "," + propColl["samaccountname"][0].ToString();
                        if (propColl["name"].Count > 0)
                            orz += "," + propColl["name"][0].ToString();
                        if(propColl["mail"].Count > 0)
                            orz += "," + propColl["mail"][0].ToString();
                        sRe += orz + "=";
                    }
                }
                if (sRe.Length > 0)
                    sRe = sRe.Substring(0, sRe.Length - 1);
                return sRe.Split('=');
            }
    
            
    
            private string[] GetOrganizationalUnit(string domainADsPath, string username, string password,string schemaClassNameToSearch)
            {
                SearchResultCollection results = _ADHelper(domainADsPath, username, password, schemaClassNameToSearch);
                string[] sRe = GetGetOrganizationalUnitResults(results);
    
                results.Dispose();
    
                return sRe;
            }
    
            private static SearchResultCollection _ADHelper(string domainADsPath, string username, string password, string schemaClassNameToSearch)
            {
                DirectorySearcher searcher = new DirectorySearcher();
    
                searcher.SearchRoot = new DirectoryEntry(domainADsPath,
                                                            username, password);
                searcher.Filter = "(objectClass=" + schemaClassNameToSearch + ")";
    
                searcher.SearchScope = SearchScope.Subtree;
                searcher.Sort = new SortOption("name",
                                                    SortDirection.Ascending);
                searcher.PageSize = 512;
    
                searcher.PropertiesToLoad.AddRange(new string[] { "name", "Path", "displayname", "samaccountname", "mail" });
    
                SearchResultCollection results = searcher.FindAll();
                return results;
            }
    
            private string[] GetGetOrganizationalUnitResults(SearchResultCollection results)
            {
                string sRe = string.Empty;
                if (results.Count == 0)
                    throw new Exception("There aren't any ou");
                else
                {
                    foreach (SearchResult result in results)
                    {
                        if (result.Path.IndexOf("OU=User") < 0)
                            continue;
                        ResultPropertyCollection propColl = result.Properties;
                        sRe += propColl["name"][0].ToString() + ',';
                    }
                }
                if (sRe.Length > 0)
                    sRe = sRe.Substring(0, sRe.Length - 1);
                return sRe.Split(',');
            }

    Thanks

    Best Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 24, 2013 1:37 AM