2

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.

Alex Le
  • 23
  • 3

3 Answers3

2

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?

Community
  • 1
  • 1
Vikram Singh Saini
  • 1,749
  • 3
  • 22
  • 42
1

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 
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1

You can:

((DirectorySearcher)myPrincipalSearcher.GetUnderlyingSearcher()).SizeLimit = 20;
Brett
  • 51
  • 2