1

I am trying to fetch all the users from AD server. There are 7000 users in AD server but while I am running my java code it is returning only 1000 user names. Is there any restriction that it will fetch only max 1000 users at a time ? Could any one please tell me how can I fetch all the 7000 users at a time.

Here is Java Code -

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;


public class FetchAllUsers {


public static void main(String args[])
{
  String username = "username";

  String password = "password";

  String ldapURL = "url";

  String contextFactory = "com.sun.jndi.ldap.LdapCtxFactory";

  String securityAuthentication = "simple";

  fetchUserList(username,password,ldapURL,contextFactory,securityAuthentication);
}


public static void fetchUserList(String username,String password,String ldapURL,String contextFactory,String securityAuthentication) {

  Hashtable<String, String> env = new Hashtable<String, String>();

  env.put(Context.INITIAL_CONTEXT_FACTORY, contextFactory);

  // set security credentials
  env.put(Context.SECURITY_AUTHENTICATION, securityAuthentication);
  env.put(Context.SECURITY_PRINCIPAL, username);
  env.put(Context.SECURITY_CREDENTIALS, password);

  // connect to my domain controller
  env.put(Context.PROVIDER_URL, ldapURL);

  try {

    List<String> usersList = new ArrayList<String>();

    LdapContext ctx = new InitialLdapContext(env, null);

    SearchControls searchCtls = new SearchControls();

    searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    // specify the LDAP search filter
    String searchFilter = "(&(objectCategory=user))";

 // Specify the Base for the search
    String searchBase = "DC=domain,DC=com";

    // initialize counter to total the results
    int totalResults = 0;

    // Search for objects using the filter
    NamingEnumeration<SearchResult> fetchData = ctx.search(searchBase, searchFilter, searchCtls);

    // Loop through the search results
    while (fetchData.hasMoreElements()) {
      SearchResult sr = (SearchResult) fetchData.next();
      totalResults++;

        String names[] = sr.getName().split(",");
        String name[] = names[0].split("=");
        usersList.add(name[1]);

    }
    System.out.println("Total number of users in AD server : " + totalResults);
    System.out.println(usersList);

  } catch (NamingException e) {
    e.printStackTrace();
  } catch (Exception e) {
    e.printStackTrace();
  }
 }
 }
ankit
  • 380
  • 4
  • 16
  • easy way is to loop 7 times. – Scary Wombat Dec 08 '15 at 07:02
  • 1
    You can either take the Wombat's advice, or you can request that the AD admin change the registry max page size to match your needs, q.v. [this article](http://blog.scottlowe.org/2008/04/11/ad-integration-tip-dealing-with-more-than-1000-users/). – Tim Biegeleisen Dec 08 '15 at 07:04
  • 1
    if you google "active directory ldap query limit 1000" you find a lot of matching hits. Have you tried one of those? It's primarliy about the MaxPageSize setting in the AD controller. See http://blog.scottlowe.org/2008/04/11/ad-integration-tip-dealing-with-more-than-1000-users/ –  Dec 08 '15 at 07:05
  • or this? http://stackoverflow.com/a/31750628/384674 – Betlista Dec 08 '15 at 07:09
  • 1
    @TimBiegeleisen I changed the default size from 1000 to as per my requirement like 7000 and it worked. Thanks for your suggestion. – ankit Dec 08 '15 at 08:53
  • @AlexanderTruemper thanks to you. I did the same and worked fine. – ankit Dec 08 '15 at 08:53
  • 1
    @Betlista I gone through your suggestion and found int pageSize = 5; ctx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.NONCRITICAL) }); this also worked fine but till 1000 only so if I am giving 7000 or any value more than 1000 then it will fetch only 1000 records because of default size is 1000 so working of these 2 lines of code must need to change registry maxPageSize to according to our requirement. Thanks to you. – ankit Dec 08 '15 at 08:57

0 Answers0