using (DirectorySearcher srch = new DirectorySearcher(String.Format("(memberOf= {0})",p_Target.DistinguishedName)))
{
srch.PageSize = 2;
SearchResultCollection results = results = srch.FindAll();
int count = results.Count;
}
count = 3 (THREE) and not 2. Why is that? I don't want to have all results in just one page. I know that PageSize = 2 is silly small but I set that value in this case just for testing purpose (in reality it will be more).
The pageSize is to set the number of records returned in one paged search. Paged search is an underlying thing at LDAP protocol level. It's transparent to you. Although you set the PageSize to 2, DirectorySearcher will return all the results for you but in your case in two paged search reply packets.
To do what you want, you should use SizeLimit instead. It will control how many records returned in total.
Here is one more tricky thing. Windows Server has a limit set on the server side. In each of the paged search result, it can only return at most 1000 entries. So, you need to be careful setting the PageSize and SizeLimit if you have the results more than 1000 entries. If you set PageSize=0 (meaning unlimited) and SizeLimit=0 (meaning unlimited), you will get an error because Windows server cannot return you more than 1000 entries in one single page. If you set Pagesize = 800 and SizeLimit=0 (meaning unlimited), you will get all your result and if you look at the network sniffer, you will see there are a bunch of LDAP paged search result. In each of the paged search result, you see 800 entries.
EDIT
Here is a more elabrated reply to the question in your comment.
Hm, interesting. Please help me understand better this mechanism: if in AD I have 5000 rows, PageSize of DirectorySearcher is set at 1000, SizeLimit is set to 0 and max server limit is 1000. How many call of directorySearcher.FindAll() I need to have in my code to get all 5000 results? 5 or 1
Regardless of how many number of records going to be returned, you always need only 1 call on DirectorySearcher. DirectorySearcher will handle the rest for you. It will aggregate the paged search result and present to you in one single IEnumerable even though the data might be from different reply packets. I guess you want to set PageLimit because you don't want all 5000 results returned all at once and occupy your memory. Don't worry on that. DirectorySearcher won't store all 5000 results in your memory as long as you don't hold reference on each of the returned SearchResult. It won't wait till all reply packets coming back either. As soon as the first reply packet coming back, the FindAll() returns the result to you. If your program is so fast that after you process the 1000 results, the second paged search result packet has still not yet arrived. The call on the MoveNext() will be blocked and wait till the second paged search result packet recevied.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With