locked
Retrieve all users from active directory RRS feed

  • Question

  • User-2137316273 posted

    Hi

    I am trying to retrieve all the users from active directory into a sorted list, but for some reason it does not return all the users like myself for instance. It's on a client's ad and there is probably about 1500+ users in AD.

    My code is as follows(Please ignore code that is not relevant. My apologies)

    public SortedList ReadFromActiveDirectoryUsers()
        {
          XPathNavigator xnMyForm = this.CreateNavigator();
          XmlNamespaceManager ns = this.NamespaceManager;
    
          string xmlADPath = string.Empty;
          string xmlADUser = string.Empty;
          string xmlADPw = string.Empty;
    
          string xmlFile = Path.Combine(SPUtility.GetGenericSetupPath(@"TEMPLATE\LAYOUTS\"), "Config.XML");
          ReadXMLFile xmlReader = new ReadXMLFile(xmlFile);
    
          xmlADPath = xmlReader.ReadValue("AdConnection", "ADPath", false);
          xmlADUser = xmlReader.ReadValue("AdConnection", "ADUser", false);
          xmlADPw = xmlReader.ReadValue("AdConnection", "ADPass", false);
    
                
          xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " XMLADPath = " + xmlADPath);
          xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " XMLADPass = " + xmlADPw);
          xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " XMLADUser = " + xmlADUser);
    
          SortedList UsersInGroup = new SortedList(); 
    
          {
          string strError = "";
          
          try
          {
            
            SortedList AllADUsers = new SortedList();
            SortedList AllADUsersReversed = new SortedList(new InverseComparer());
    
            DirectoryEntry de = new DirectoryEntry(xmlADPath, xmlADUser, xmlADPw);
            de.AuthenticationType = AuthenticationTypes.Delegation;
            DirectorySearcher deSearch = new DirectorySearcher(de);
            //deSearch.Filter = "objectCategory=user"; // Show only users
            deSearch.Filter = "(&(objectCategory=user)(sAMAccountName=*))";
            //deSearch.Filter = "objectClass=user"; // Include PC and groups
            SearchResultCollection results = null;
    
            
    
            try
            {
              results = deSearch.FindAll();
            }
            catch (Exception ex)
            {
              strError += ex.Message;
            }
            //if the group is valid, then continue, otherwise return a blank dataset
            int errorCounter = 0;
            int userCounter = 0;
    
            string strGivenName = "";
            string strSurname = "";
            string sam = "";
            string DisplayName = "";
            string Displayname2 = "";
    
            bool blnFirstRun = true;
    
            foreach (SearchResult user in results)
            {
              bool blnAdded = false;
    
              xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " Entered foreach SearchResult user in results");
    
              userCounter += 1;
    
                //AllADUsers.Add(user.GetDirectoryEntry().Properties["sAMAccountName"].Value.ToString(), user.GetDirectoryEntry().Properties["name"].Value.ToString() + " | ");
    
    
                  //sam = xmlDomain + "\\" + deUser.Properties["sAMAccountName"].Value.ToString();
    
              if (user.GetDirectoryEntry().Properties["sAMAccountName"].Count > 0)
              {
                sam = user.GetDirectoryEntry().Properties["sAMAccountName"].Value.ToString();
    
                xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " Entered if (user.GetDirectoryEntry().Properties[sAMAccountName].Count > 0)");
    
                if (user.GetDirectoryEntry().Properties["givenName"].Count > 0)
                {
                  strGivenName = user.GetDirectoryEntry().Properties["givenName"].Value.ToString();
                }
                else
                {
                  strGivenName = "...";
                }
    
                xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " strGivenName = " + strGivenName + " ");
    
                if (user.GetDirectoryEntry().Properties["sn"].Count > 0)
                {
                  strSurname = user.GetDirectoryEntry().Properties["sn"].Value.ToString();
                }
                else
                {
                  strSurname = "...";
                }
    
                xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " strSurname = " + strSurname + " ");
    
                xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " sam = " + sam + " ");
    
                DisplayName = strGivenName + " " + strSurname;
    
                
    
                xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " DisplayName = " + DisplayName);
    
                //AllADUsers.Add(DisplayName + sam, DisplayName);
    
              }
            
    
              DisplayName = strGivenName + " " + strSurname;
    
              if (DisplayName == "... ...")
              {
                DisplayName = sam + " ...";
              }
    
              Displayname2 = strGivenName + strSurname;
    
              xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " adding all users ");
              
              AllADUsers.Add(Displayname2 + sam, DisplayName);
    
              xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " Finished adding all users");
              
            }
            strError += "Number of users found:" + userCounter;
            strError += "Number of errors found:" + errorCounter;
    
            foreach (DictionaryEntry entry in AllADUsers)
            {
              AllADUsersReversed.Add(entry.Key, entry.Value);
            }
    
            return AllADUsersReversed;
          }
          catch (Exception exx)
          {
            SortedList AllADUsersEmpty = new SortedList();
            strError += "Exception error::" + exx.Message;
    
            xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).SetValue(xnMyForm.SelectSingleNode("/my:myFields/my:secHidden/my:txtDebug", ns).Value + " Error: = " + strError);
    
            return AllADUsersEmpty;
          }
        }      
       }
    

    What am I doing wrong?

    This is the first time I am working with AD...

     

    Please help

    Wednesday, July 7, 2010 4:56 AM

Answers

  • User197322208 posted

    try

    deSearch.SizeLimit = System.Int32.MaxValue;

    deSearch.PageSize = System.Int32.MaxValue;

    (by default sizelimit is 1000

    http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.sizelimit.aspx)


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 7, 2010 7:11 AM

All replies

  • User197322208 posted

    try

    deSearch.SizeLimit = System.Int32.MaxValue;

    deSearch.PageSize = System.Int32.MaxValue;

    (by default sizelimit is 1000

    http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.sizelimit.aspx)


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 7, 2010 7:11 AM
  • User-1115275302 posted

    I got a requirement to retrieve all users from active directory using asp.net.

    I am new to active directory.

    so can anyone help me out?

    Monday, August 19, 2013 5:07 AM
  • User1508394307 posted

    DirectoryEntry domain1;
    DirectorySearcher searcher1;
    List<string> lista1 = new List<string>();

    dominio = new DirectoryEntry("LDAP://test.com/DC=dept1,DC=com", "domainadmin", "12345", AuthenticationTypes.ReadonlyServer);

    searcher1= new DirectorySearcher("(&(objectCategory=Person)(objectClass=user)");
    searcher1.SearchRoot = domain1;
    searcher1.SearchScope = SearchScope.Subtree;
    SearchResultCollection results1;
    results1= searcher1.FindAll();
    for (int i = 0; i < results1.Count; i++)
    {

    lista1 .Add(results1[i].Properties["samaccountname"][0].ToString());
    }

    http://forums.asp.net/t/1333847.aspx/1

    Tuesday, August 20, 2013 1:59 AM
  • User-1115275302 posted

    I have used ur code.

    the thing is that i have got the server ip address as some 102.33.323.208:801, domain name as some abc.local, username and password. the server is located remotely.

    can anyone help me with the AD path?

    Tuesday, August 20, 2013 9:30 AM
  • User1508394307 posted

    If you unable connect using "LDAP://test.com/DC=dept1,DC=com" you need to contact your network administrator. Each setup is different and if your server is not the forest most likely you will not be able to connect it.

    Tuesday, August 20, 2013 9:48 AM