Can I get more than 1000 entries from DirectorySearcher?

I just noticed that the list of results for the results is limited to 1000. I have more than 1000 groups in my domain (HUGE domain). How can I get more than 1000 entries? Can I start with a later recording? Can I cut it into multiple requests?

Here is my request:

DirectoryEntry dirEnt = new DirectoryEntry("LDAP://dhuba1kwtn004"); string[] loadProps = new string[] { "cn", "samaccountname", "name", "distinguishedname" }; DirectorySearcher srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps); var results = srch.FindAll(); 

I tried to set srch.SizeLimit = 2000; but it does not work. Any ideas?

+66
c # active-directory
Sep 18 '08 at 7:11
source share
1 answer

You need to set DirectorySearcher.PageSize to a non-zero value to get all the results.

By the way, you should also get rid of DirectorySearcher when you are done with it.

 using(var srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps)) { srch.PageSize = 1000; var results = srch.FindAll(); } 

The API documentation is not very clear, but essentially:

  • when you do a pagination, SizeLimit is ignored, and all matching results are returned when you iterate over the results returned by FindAll. Results will be received from the server page at a time. I chose a value of 1000 above, but you can use a lower value if you want. Compromise: using a small PageSize will return every page of results faster, but will require more frequent server calls when iterating through a large number of results.

  • by default, the search is not paginated (PageSize = 0). In this case, the result is returned before SizeLimit.

As Biri pointed out, it is important to get rid of the SearchResultCollection returned by FindAll, otherwise you may encounter a memory leak, as described in the "Notes" section of the MSDN documentation for DirectorySearcher.FindAll .

One way to avoid this in .NET 2.0 or later is to write a wrapper method that automatically removes SearchResultCollection. It might look something like this (or it could be an extension method in .NET 3.5):

 public IEnumerable<SearchResult> SafeFindAll(DirectorySearcher searcher) { using(SearchResultCollection results = searcher.FindAll()) { foreach (SearchResult result in results) { yield return result; } } // SearchResultCollection will be disposed here } 

Then you can use it as follows:

 using(var srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps)) { srch.PageSize = 1000; var results = SafeFindAll(srch); } 
+168
Sep 18 '08 at 7:15
source share



All Articles