Alex Le
Alex Le

Reputation: 23

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.

Upvotes: 2

Views: 2534

Answers (3)

Brett
Brett

Reputation: 51

You can:

((DirectorySearcher)myPrincipalSearcher.GetUnderlyingSearcher()).SizeLimit = 20;

Upvotes: 1

marc_s
marc_s

Reputation: 755157

You need to get the underlying DirectorySearcher and set the PageSize property on it:

using (PrincipalContext ad = new PrincipalContext(contextType, adserviceName, adContext, ContextOptions.SimpleBind, username, password))
{ 
    UserPrincipal u = new UserPrincipal(ad) {Name = "*"};

    PrincipalSearcher search = new PrincipalSearcher { QueryFilter = u };

    // get the underlying "DirectorySearcher"
    DirectorySearcher ds = search.GetUnderlyingSearcher() as DirectorySearcher;

    if(ds != null)
    {
        // set the PageSize, enabling paged searches
       ds.PageSize = 500;
    }


    foreach (var principal in search.FindAll()) 
    {
        //do something 
    }
}

Upvotes: 1

Vikram Singh Saini
Vikram Singh Saini

Reputation: 1887

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?

Upvotes: 2

Related Questions