DirectorySearcher and SizeLimit

Seems like each time you actually wants to use some MS technology for real you find a serious bug in it…

If you query active directory to get a list of say, all the machines on the network and the list happens to be bigger than a 1000 items there is a problem using DirectorySearcher. There is a property called SizeLimit but even if you set it to anything larger than a 1000 it always only return a 1000 items.

Fortunately (like a lot of other cases) there is a way around it. DirectorySearcher have another property for PageSize which if you set that then the size limit is ignored and you can access more than the 1000 items.

e.g.

string filter = “(objectCategory=Computer)”;
using (DirectorySearcher searcher = new DirectorySearcher(filter))
{
//searcher.SizeLimit = 2000; //ignored
searcher.PageSize = 1000;
SearchResultCollection matches = searcher.FindAll();
foreach (SearchResult match in matches)
{
DirectoryEntry de = match.GetDirectoryEntry();

}
}

Leave a Reply

%d bloggers like this: