0 out of 3 rated this helpful - Rate this topic

DirectorySearcher.PageSize Property

Gets or sets a value indicating the page size in a paged search.

Namespace:  System.DirectoryServices
Assembly:  System.DirectoryServices (in System.DirectoryServices.dll)
[DSDescriptionAttribute("DSPageSize")]
public int PageSize { get; set; }

Property Value

Type: System.Int32
The maximum number of objects the server can return in a paged search. The default is zero, which means do not do a paged search.
Exception Condition
ArgumentException

The new value is less than zero.

After the server has found the number of objects that are specified by the PageSize property, it will stop searching and return the results to the client. When the client requests more data, the server will restart the search where it left off.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
SizeLimit relation to PageSize
SizeLimit  |  PageSize  |  Results Return
-----------|-------------|-----------------
1005        |  1100         |  1005(?)

tested it and apparently returned 1000 to me. So when PageSize > SizeLimit and SizeLimit > 1000 it returnes the server-determined default (1000).
Helpful..
Thanks for the additional community content - MS Examples are 'barebones' at times - this saved me enough time to justify adding an example usage from one of my wrapper functions

public SearchResultCollection SearchDirectory(string FilterString = "", DirectoryEntry StartFromHere = null,int MaxResults=Int32.MaxValue-1)
   {
   if (StartFromHere == null)
   { StartFromHere = new DirectoryEntry(CurrentDomainProperties.ADLDAPRootNodeSearchPath()); }
   DirectorySearcher searcher = new DirectorySearcher(StartFromHere);
   
   searcher.SizeLimit = MaxResults;
   searcher.PageSize = 1000;
   searcher.Filter = string.Format(FilterString);
  ......

This quite happily returns 5000+ results on my machine..
SizeLimit relation to PageSize
Since it was unclear i figured a chart might be the easiest way to explain how SizeLimit and PageSize relate to each other.  If the PageSize is greater than the SizeLimit you will retrieve up to the SizeLimit.  If the SizeLimit is greater than the PageSize you will retrieve all matching results. If the PageSize is the default of "0" then you will retrieve the SizeLimit OR 1000, whichever is smaller.  Personally I think there is a bug in how this works cause it was against my, and possible others expectations of how this work.  I added the last line where the PageSize is over 1000, but don't have data to test whether or not that returns 1005 results or if it errors out cause PageSize has a limit.

SizeLimit  |  PageSize  |  Results Return
-----------|-------------|-----------------
50            |  0               |  50
50            |  100           |  50
50            |  5               |  All
1005        |  0               |  1000
1005        |  1               |  All
1005        |  50             |  All
1005        |  1100         |  1005(?)
Only getting 1000 search results?
What this documentation fails to mention is that if you do not set the PageSize property (or if you set it to 0) then you will only ever get a maximum of 1000 search results brought back by the DirectorySearcher.FindAll method. It also does not make it very clear that you dont actually have to do anything to request more data in a paged search - simply set the PageSize property to 1000 and everything is taken care of for you.