title: How to fix Ldap connection error "javax.naming.SizeLimitExceededException: [LDAP: error code 4 - Sizelimit Exceeded]" in Java?
body: <p>There are 7563 Employees in AD. I need to pick them all using (employeeId=*)</p>
  
  <p>Following is my Java code for the same:</p>
  
  <pre><code>        Hashtable&lt;String, String&gt; ldapEnv = new Hashtable&lt;&gt;();
          ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
          ldapEnv.put(Context.PROVIDER_URL, PROVIDER_URL);
          ldapEnv.put("com.sun.jndi.Ldap.connect.timeout", CONNECTION_TIMEOUT); // specify timeout out to be 5 seconds
          ldapEnv.put(Context.SECURITY_AUTHENTICATION, AUTHENTICATION_TYPE);
          ldapEnv.put(Context.SECURITY_PRINCIPAL, AD_USER);
          ldapEnv.put(Context.SECURITY_CREDENTIALS, AD_PASS);
          DirContext context = new InitialDirContext(ldapEnv); // initial context created
          System.out.println("(employeeID=" + aDemployeeID + ")");
          String searchFilter = "(employeeID=*)";
          SearchControls controls = new SearchControls();
          String[] requiredAttribute = { "mail", "Targetaddress", "UserPrincipalName" };
          controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
          controls.setReturningAttributes(requiredAttribute);
          controls.setCountLimit(1200);
  
          NamingEnumeration&lt;SearchResult&gt; users1 = context.search(SEARCH_DIRECTORY_1, searchFilter, controls);
  
          adAttributesToEVRYIntegrator = new HashMap&lt;&gt;();
          int count = 0;
          while (users1.hasMore()) {
              SearchResult sr = users1.next();
  
              NamingEnumeration&lt;? extends Attribute&gt; aDattributes = sr.getAttributes().getAll();
              count = count + 1;
              while (aDattributes.hasMore()) {
                  Attribute requestedAttributesFromAD = aDattributes.next();
                  System.out.println(requestedAttributesFromAD.get().toString());
              System.out.println(count);
  
          }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code> After 1000 Records:
   javax.naming.SizeLimitExceededException: [LDAP: error code 4 - Sizelimit Exceeded]; remaining name 'OU=Users,OU=Sweden,OU=CCD,OU=Customers,DC=corp,DC=corpcommon,DC=com'
  </code></pre>
  
  <p>I have checked in multiple folder and i found that: </p>
  
  <ul>
  <li>By Default, LDAP is returning 1000 Records.</li>
  </ul>
  
  <p>Could anyone suggest how to get all records from LDAP</p>
  
  <ul>
  <li>In Single Go</li>
  <li>In Batches (as i do not have employee Id's with me so how to check that records already came does not come again when using *)</li>
  </ul>
  
  <p>PS: I have done all changes in related posts but not able to fix the problem.</p>