0
votes

I found the following article :

https://developers.google.com/admin-sdk/directory/v1/quickstart/apps-script

which instructs you to get all users from your google domain. I'd like to get ALL of the users however, but maxResults seems to limit the results to a maximum of 500 result entries. How can I bypass this and get all users? I'm using the following code.

  function listUsers() {
  var optionalArgs = {
    customer: 'my_customer',
    maxResults: 5000,
    orderBy: 'email'
    
  };
  var response = AdminDirectory.Users.list(optionalArgs);
  var users = response.users;
  if (users && users.length > 0) {
    Logger.log(users.length);
    Logger.log('Users:');
    for (i = 0; i < users.length; i++) {
      var user = users[i];
      Logger.log('%s (%s)', user.primaryEmail, user.name.fullName);
    }
  } else {
    Logger.log('No users found.');
  }
}
1
Where do you see a limit of 500? I don't see a limit specified in the documentation, but I do see that the results are paginated. Are you retrieving all of the paginated results? - Diego
I've updated with my code but I'm still getting the limit of 500. Even though I'm not paginating the results. - Marko Marinkovic
You haven't updated the minimal reproducible example - Cooper

1 Answers

1
votes

You need to get the paginated results by including the nextPageToken from the response in the parameters of subsequent calls as pageToken.

function getAllUsers() {
  var optionalArgs = {
    domain: 'example.com',
    orderBy: 'email'
  };
  var response = {};
  var users = [];

  do {
    optionalArgs.pageToken = response.nextPageToken;
    response = AdminDirectory.Users.list(optionalArgs);
    users = users.concat(response.users);
  } while (response.nextPageToken);
  
  return users;
}