0

I'm using System.DirectoryServices.DirectorySearcher for traversing Active Directory for users. In most cases, this works fine. The code is pretty much as follows:

Ldapconnection conn = CreateConnection(...);
System.DirectoryServices.DirectorySearcher searcher = new System.DirectoryServices.DirectorySearcher();
searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;

foreach (System.DirectoryServices.SearchResult result in searcher.FindAll()) {
}

For some reason, this doesn't always traverse the tree completely. If I specify the searchroot to point at a missing subtree, it does import that subtree.

Am I missing something?

TylerH
  • 20,799
  • 66
  • 75
  • 101
kh34
  • 181
  • 1
  • 3
  • 11
  • When it doesn't work as expected, is the SearchResultCollection returned by the FindAll method count 1000? – dugas Mar 25 '11 at 17:27

2 Answers2

2

Is it stopping at 1000 users? I believe AD is setup by default to only return 1000 objects.

scottrudy
  • 1,633
  • 1
  • 14
  • 24
  • First, I only got around 150 users. Second attempt around a thousand, actually. So my second attempt might actually be correct, but I hit the limit. Any way to work around that? – kh34 Mar 27 '11 at 08:42
1

If the SearchResultCollection's count is 1000 in the cases that you experience the problematic behavior, try setting the PageSize property on the DirectorySearcher to 1000.

Ldapconnection conn = CreateConnection(...); 
DirectorySearcher searcher = new System.DirectoryServices.DirectorySearcher();
searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
searcher.PageSize = 1000;

foreach (System.DirectoryServices.SearchResult result in searcher.FindAll()) { } 
dugas
  • 12,025
  • 3
  • 45
  • 51