When you are calling DirectorySearcher.FindAll() and there is a chance to have more than 1000 records back, you must set PageSize to non-zero value, preferably 1000. Otherwise only the first 1000 records will be returned and other entries will be missed without any warning.
The names of DirectorySearcher members and documentation is quite messy. FindAll() method should return ALL records, not the first 1000. Setting PageSize doesn’t mean that you get back only single page, but triggers returning of the values into “page size” chunks transparently to you in the background. If you set SizeLimit to a value that is larger than
the server-determined default of 1000 entries, the server-determined default is used.
Thanks to Joe Kaplan’s post that finally helped to find solution.
I have a simple program that imported AD users to application database. It was tested and worked fine on a few sites.
However one of our customers complained that some users were missed during import when others were imported successfully. No pattern was found what is the difference between imported and missed user accounts.
After some investigation it was noticed that only 1000 records are returned by DirectorySearcher.FindAll. Ok, documentation describes DirectorySearcher.SizeLimit with default value is zero, which means to use the server-determined default size limit of 1000 entries. I’ve changed SizeLimit to 100000, but it didn’t help -still only the first 1000 was returned.
Reading further, I found that the server-determined default is used so SizeLimit is useless, if you expect big number of results. In a few posts I found how to change the default value on the server, but there were also recommendations not to change server setting, but use paging instead. OK but how to use paging? I’ve started to search for code examples, expecting that some complicated coding will be required.
Finally Joe Kaplan’s post explains that only code change that required is
and all paging will happen in background.
MS should have better DirectorySearcher documentation and implement background paging by default.