DirectorySearch.PageSize = 2 does not work

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), not 2. Why? I do not want to have all the results on just one page. I know that PageSize = 2 is stupidly small, but I set this value in this case only for testing purposes (in reality it will be more).

+4
source share
2 answers

The pageSize parameter specifies the number of records returned in a single search call. Page search is a basic thing at the LDAP protocol level. It is transparent to you. Although you set PageSize to 2, DirectorySearcher will return all the results for you, but in your case, in two page search packages.

To do what you want, you must use SizeLimit. It will control the number of records returned.

Here's another tricky thing. Windows Server has a server-side restriction. In each of the results of the search call, it can return no more than 1000 records. Therefore, you should be careful when setting up PageSize and SizeLimit if you have results of more than 1000 entries. If you set PageSize = 0 (which means unlimited) and SizeLimit = 0 (which means unlimited), you will receive an error message because the Windows server will not be able to return you more than 1000 entries on one page. If you set Pagesize = 800 and SizeLimit = 0 (which means unlimited), you will get all your result, and if you look at the network sniffer, you will see that there are many search results for LDAP pages. In each of the search results you see 800 entries.

EDIT

Here is a more useful answer to the question in your comment.

Hm, interesting. please help me better understand this mechanism: if I have 5000 lines in AD, PageSize DirectorySearcher is set to 1000, SizeLimit is set to 0 and the maximum server limit is 1000. How many calls directorySearcher.FindAll () I need to have in my code to get all 5000 results? 5 or 1

No matter how many records are returned, you always need only one call to DirectorySearcher. DirectorySearcher will handle the rest for you. It will aggregate the search result across pages and present you in one IEnumerable, even if the data can be from different response packets. I think you want to install PageLimit because you do not want all 5000 results to return immediately and take up your memory. Do not worry about it. DirectorySearcher will not store all 5000 results in your memory if you do not keep a link to each returned SearchResult. It will not wait until all response packets are returned. As soon as the first response packet is returned, FindAll () returns the result to you. If your program is so fast that after processing the results of 1000, the second batch of search results still does not appear. The call to MoveNext () will be blocked and wait until the second packet of search results is received.

+5
source

Look at this code and related article by Ethan Wilansky,

http://msdn.microsoft.com/en-us/magazine/dd250826.aspx?code=true&level=root&file=VLVSrch.cs

That you want.

0
source

All Articles