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; } }
Then you can use it as follows:
using(var srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps)) { srch.PageSize = 1000; var results = SafeFindAll(srch); }
Joe Sep 18 '08 at 7:15 2008-09-18 07:15
source share