retrieving 1000+ users from AD group using C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drastogi
    New Member
    • Jan 2016
    • 4

    retrieving 1000+ users from AD group using C#

    I am trying to retrieve 5000 users from AD group, but when I set PageSize= 1000 (or let's say 15000) and SizeLimit=15000 , I still get only 1500 users. Any other configuration that I can set (I want to avoid range retrieval as suggested by MS) Please advice. Thanks Below is the code section: Please let me know what am I missing.
  • drastogi
    New Member
    • Jan 2016
    • 4

    #2
    Code:
    try
    {
    searcher.SizeLimit=15000;
    searcher.PageSize = 1000; 
    return searcher.FindAll();
    }
    Last edited by Rabbit; Jan 7 '16, 10:50 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

    Comment

    • Luk3r
      Contributor
      • Jan 2014
      • 300

      #3
      What is your full code? It appears you likely have a problem with the search string, assuming there really is 5000 users and not only 1500.

      Comment

      • drastogi
        New Member
        • Jan 2016
        • 4

        #4
        Code:
        private SearchResultCollection ExecuteBestEffortSearch(DirectorySearcher searcher)
            {
              if (searcher == null) { throw new ArgumentNullException("searcher"); }
              if (searcher.SearchRoot == null) { throw new ArgumentException("Directory Searcher does not have a search root.", "searcher"); }
        
              // Construct Search Message
              string message = String.Format("[{0}] ({1}) with filter [{2}] for attributes [{3}]",
                searcher.SearchRoot.Path, searcher.SearchScope, searcher.Filter, FlattenStringCollection(searcher.PropertiesToLoad));
        
              // Execute Search
              try
              {
                if (Logger.DebugLevel >= Logger.MaxDebugLevel)
                {
                  Logger.Debug(0, "Executing directory search for " + message, null, DirectoryServiceException.ModuleName, Logger.MaxDebugLevel);
                }
                        searcher.PageSize = 1000; //a positive number to turn off the default page limit in DirectorySearcher
                return searcher.FindAll();
        
               }
              catch (COMException adsiException)
              {
                DirectoryServiceException dsExeception = DirectoryServiceException.Create(
                  String.Format("The directory search {0} was unable to execute due to a directory service error.", message), adsiException);
        
                if (dsExeception is DirectoryServerUnavailableException)
                {
                  try
                  {
                    Guid searchRootGuid = searcher.SearchRoot.Guid;
                    // Cleanup resources
                    searcher.SearchRoot.Dispose();
                    // Attempt to Re-Bind
                    using (DirectoryEntry searchRootEntry = GetEntryByGuid(searchRootGuid))
                    {
                                    searcher.PageSize = 1000; //a positive number to turn off the default page limit in DirectorySearcher
                      searcher.SearchRoot = searchRootEntry;
                      return searcher.FindAll();
                    }
                  }
                  catch (Exception ex)
                  {
                      Logger.LogError(30019, String.Format("The directory search {0} failed because the search root was unavailable and the server was not able to establish a new bind.", message), ex, DirectoryServiceException.ModuleName, Logger.MinDebugLevel);
                  }
                }
                throw dsExeception;
              }
            }
        Last edited by Rabbit; Jan 7 '16, 10:50 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

        Comment

        • drastogi
          New Member
          • Jan 2016
          • 4

          #5
          And SizeLimit i am defining when i create an object of DirectorySearch er class

          Comment

          • Luk3r
            Contributor
            • Jan 2014
            • 300

            #6
            I'm still not seeing in your code where you supply a search filter to only grab user objects.

            Comment

            Working...