0

In below code i want the user details to be shown more then 500 records. is that possible. Thanks for any help.

page = AdminDirectory.Users.list({
                domain : 'domain name',
                orderBy : 'givenName',
                maxResults: 500,
                pageToken : pageToken
            });
tehhowch
  • 9,645
  • 4
  • 24
  • 42
  • Well, what have you tried? Have you [looked at the API call description](https://developers.google.com/admin-sdk/directory/v1/reference/users/list)? Where is your code calling this? Consider also that you might need to [perform search queries](https://developers.google.com/admin-sdk/directory/v1/guides/search-users), rather than just blindly listing, when you have many members of a domain... – tehhowch Jan 13 '20 at 12:07
  • @tehhowch. iam trying to get "user.name.fullName, user.primaryEmail, user.orgUnitPath" from AdminDirectory. My code is running good. But only issue is that iam getting only 500 Records. The api you mentioned has maxResults limit of 500 records.The search query brings more then 500 records. Iam new to google script. Pl let me know if anything is missing. " List more than 30 students in a class in Google Classroom API query" is not the solution. :) – Manju Parihar Jan 13 '20 at 12:13
  • Use a query that returns less than the maximum number of results? As you state, that's the **maximum** number of results... – tehhowch Jan 13 '20 at 12:31
  • Hey @ManjuParihar did the answer I provided you help you? I think that adapting your code to the API's pagination system is the only way to go with it... Cheers – carlesgg97 Jan 13 '20 at 14:59

1 Answers1

1

The endpoint does not return more than 500 results as a maximum per call. Instead, you should implement pagination in your code. In order to retrieve every single user in your domain, you can use the following code:

function getAllUsers() {
  var pageToken;
  var result = [];

  do {
    var page = AdminDirectory.Users.list({
                 domain : 'domain name',
                 orderBy : 'givenName',
                 maxResults: 500,
                 pageToken : pageToken
               });
    result = result.concat(page.users);
    pageToken = page.nextPageToken;
  } while (pageToken);

  return result;
}

The idea is that each request that is not complete (returns users, but there are still more users to retrieve) a pageToken is returned. For the next request, you can use the token to retrieve the users that have not been retrieved yet, and so on until the returned pageToken becomes null (no more users are left in the domain).

carlesgg97
  • 4,184
  • 1
  • 8
  • 24
  • 1
    OP had already seen a pagination example (I linked to [my answer here](https://stackoverflow.com/a/49338510)) and declared it not the solution – tehhowch Jan 13 '20 at 13:26
  • @tehhowch Apologies for that - I haven't seen your comment (I assume you removed it after OP's reply) so I was somewhat confused. Anyway, I wish OP would at least reply and give more info on whether this code is working/not working and what are the issues obtained if applicable... – carlesgg97 Jan 14 '20 at 12:56