Thursday, December 11, 2008

ADSI Query Limits

Have you ever used the .Net DirectorySearcher class to query an ADSI domain and run into a 1000 object limit? Say you want to get all the user accounts from a large domain. You connect to the domain and run FindAll() to grab all users, and voila, you stop at 1000 users. The documentation implies that DirectorySearcher.SizeLimit is the way to jump the 1000 limit. That does not work either. It turns out that the PageSize property solves the problem. Here is some sample code:


DirectoryEntry de = new DirectoryEntry(someAdsiPath);
DirectorySearcher ds2 = new DirectorySearcher(de, "(objectCategory=user)");
using(de)
{
using (ds2)
{
ds2.SizeLimit = 50000;
ds2.PageSize = 1000;
ds2.PropertiesToLoad.Add("sAMAccountName");
foreach (SearchResult sr in ds2.FindAll())
{
//... do stuff
}
}
}


I'm not aware of whether or not the line setting SizeLimit is necessary. PageSize fixed the issue at a customer's site, and it's a bit impractical for me to test variations on SizeLimit.

Cheers,
Chris

No comments:

Post a Comment

Please keep it short, respectful, and clean!