0

Although paging is covered in the ldapjs documentation, I'm unclear on how to implement paging in my application using an ajax client. Let's say I make a simple call to the server to search the LDAP for all people in the organization, initially for the first page with 10 entries, like this:

$.ajax({
  method: "GET",
  url: "LDAPSearch",
  data: {
    filter: "(ou=People)",
    pageNum: 1,
    entriesPerPage: 10
  }
}).done(function( result ) {
  console.log('result: ', result);
});

I would expect the server to send back a result containing the first 10 entries as well as the total number of entries that the search yielded so that I know how many pages there will be. In the ldapjs code on the server, I would expect a parameter in the opts for pageNum, such as:

var opts = {
  filter: '(objectclass=commonobject)',
  scope: 'sub',
  paged: true,
  pageNum: 1, //This is not a valid option, but how I would expect it to work
  sizeLimit: 10
};
client.search('o=largedir', opts, function(err, res) {
  assert.ifError(err);
  res.on('searchEntry', function(entry) {
    // do per-entry processing
  });
  res.on('page', function(result) {
    console.log('page end');
  });
  res.on('error', function(resErr) {
    assert.ifError(resErr);
  });
  res.on('end', function(result) {
    console.log('done ');
  });
});
Andrew
  • 2,368
  • 1
  • 23
  • 30

1 Answers1

0

You will find the needed information here.

... and will output all of the resulting objects via the searchEntry event. At the end of each result during the operation, a page event will be emitted as well...

So use the searchEntry event to add the result to a collection, for example:

res.on('searchEntry', function(entry) {
  resultArray.push(entry.object);
});

... and use the page event to continue with next page if the sizeLimit (of page) is reached.

res.on('page', function(res, cb) {
  if (cb) {
    cb.call();
  } else {
    // search is finished, results in resultArray
  }
});

Hold in mind that LDAP server often limit the search by default or configuration. 500 is a common used default value. See also http://www.openldap.org/doc/admin24/limits.html.

Manfred Steiner
  • 1,215
  • 2
  • 13
  • 27