locked
Retrieve all users from Active Directory? RRS feed

  • Question

  • 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

    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

    • Moved by Harry Zhu Wednesday, July 7, 2010 9:34 AM (From:Visual C# General)
    Wednesday, June 30, 2010 12:10 PM

All replies

  • Does anyone have an answer on this...??
    Thursday, July 1, 2010 6:03 AM
  • Hello,


    I recommend you create a new thread at
    http://forums.asp.net/93.aspx.  That is the Active Directory forum queue in ASP.NET Forums.   You will get better and quicker responses from the experts there.


    Best regards,
    Harry Zhu
    - MSFT MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com
    Tuesday, July 6, 2010 1:55 AM
  • Thank you Harry
    Tuesday, July 6, 2010 6:59 AM