Search code examples
c#active-directorymultiple-users

Can I get more than 1000 records from a PrincipalSearcher?


I am trying to get all users from Active Directory using code:

PrincipalContext ad = new PrincipalContext(contextType, adserviceName, adContext, ContextOptions.SimpleBind, username, password);
UserPrincipal u = new UserPrincipal(ad) {Name = "*"};
PrincipalSearcher search = new PrincipalSearcher { QueryFilter = u };
foreach (var principal in search.FindAll()) 
{
    //do something 
}

But it returns only first 1000 rows. How I can retrieve All users and without using DirectorySearcher. Thanks.


Solution

  • I don't think you will be able to do that without using DirectorySearcher.

    Code snippet -

    // set the PageSize on the underlying DirectorySearcher to get all entries
    ((DirectorySearcher)search.GetUnderlyingSearcher()).PageSize = 1000;
    

    Also see If an OU contains 3000 users, how to use DirectorySearcher to find all of them?