views:

3873

answers:

5

I just noticed that the return list for results is limited to 1000. I have more than 1000 groups in my domain (HUGE domain). How can I get more than 1000 records? Can I start at a later record? Can I cut it up into multiple searches?

Here is my query:

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 have tried to set srch.SizeLimit = 2000; but that doesn't seem to work. Any ideas?

+18  A: 

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

BTW you should also dispose DirectorySearcher when you're finished with it

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

The API documentation isn't very clear, but essentially:

  • when you do a paged search, the SizeLimit is ignored, and all matching results are returned as you iterate through the results returned by FindAll. Results will be retrieved from the server a page at a time. I chose the value of 1000 above, but you can use a smaller value if preferred. The tradeoff is: using a small PageSize will return each page of results faster, but will require more frequent calls to the server when iterating over a large number of results.

  • by default the search isn't paged (PageSize = 0). In this case up to SizeLimit results is returned.

As Biri pointed out, it's important to dispose the SearchResultCollection returned by FindAll, otherwise you may have a memory leak as described in the Remarks section of the MSDN documentation for DirectorySearcher.FindAll: http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher.findall.aspx

One way to help avoid this in .NET 2.0 or later is to write a wrapper method that automatically disposes the SearchResultCollection. This might look something like the following (or could be an extension method in .NET 3.5):

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

You could then use this as follows:

using(DirectorySearcher srch = new DirectorySearcher(dirEnt, "(objectClass=Group)", loadProps))
{
    srch.PageSize = 1000;
    var results = SafeFindAll(srch);
}
Joe
Excellent workaround. Worth an upvote.
Biri
This answered my question and more! Awesome advice, thank you very much!
naspinski
+3  A: 

@naspinski: I also replied on your comment on your previous post.

Not only dispose DirectorySearcher, but also dispose the SearchResultCollection as well. It is using some unmanaged resources.

Biri
A: 

While I seem to be a) setting PageSize; b) disposing of the DirectoryServices objects, I'm running into this: when I set results = searcher.FindAll() I get an error returned: "The value for PageSize cannot be set." I can't find documentation for this anywhere. Do you have any ideas? I'm searching a Lotus Notes LDAP directory; could this be authentication-related?

+1  A: 

Thank you very very much for pageSize limitation Best regards all answered

Cengiz Anıl
A: 

Hi,

That solved a big problem for me but I ran into another issue. When I iterate over the collection using the foreach loop for the first time I get right results but I get different when I iterate over the loop for the second time . SafeFindAll is a static function in my class whereas DirectorySearcher is also static. After first pass of foreach loop is complete, I call dsipose on DirectorySearcher obkect but that doesn't make any difference.

Thanks.