C# DirectorySearcher search result more than 1000

When searching Active directory, DirectorySearcher will be used to retrieve information from Active Directory.

Below is the code that gets the username information.

 

string DomainPath = "LDAP://DC=domain,DC=com";
DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = "(&(objectClass=user)(objectCategory=person))";
search.PropertiesToLoad.Add("samaccountname");
SearchResult result;
SearchResultCollection resultCol = search.FindAll();
foreach(SearchResult item in resultCol )
{
    string username = item.Properties.Contains("samaccountname");
    Console.WriteLine(username);
}

 

However, by default, only 1000 can be viewed.

To do this, you can use the property PageSize in DirectorySearcher.

It can be used as follows.

 

DirectoryEntry searchRoot = new DirectoryEntry(DomainPath);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.PageSize = 4000;

 

It is possible to input up to 32 bits INT size, but please input the appropriate number.

 

 

Facebook Comments

Leave A Reply

Detection ADBlockPlease, Disable or add to white list on our site.