4

In a helpful response to this post I learned that I can pull a maximum of 500 user results from my domain directory at once. Now I am trying to get a script to continue to run and pull batches of 500 until it pulls all the users from the domain (or, in this case, the OU) and adds them all to the same spreadsheet.

Right now my code looks like this:

function writeToSpreadsheet(){
  var values = [];
  var users = AdminDirectory.Users.list({
    domain:'klht.org',
    maxResults: 500,
    query: "orgUnitPath=/CleverTeachers/King"
  }).users; 
  for (var i=0; i<users.length; i++){
    values.push([users[i].name.fullName, users[i].primaryEmail]);   
  }
  var spreadsheetID = '1JLDD2wm0_udmTn9ZHvdKhL_Ok3SvKYFqkBeiA1GdnYc';
  SpreadsheetApp.openById(spreadsheetID).getSheets()[0].getRange(1, 1, values.length, values[0].length).setValues(values);
}

The commenter on my last post indicated "part of the returned object will be a 'nextPageToken' you can add this into a follow-on query or queries until you have all your users." He included the following snippet:

var usersPage2 = AdminDirectory.Users.list({
                                      domain: 'klht.org',
                                      maxResults: 500,
                                      query: "orgUnitPath=/OU",
                                      pageToken: nextPageToken
                                      }).users;

I found that simply appending it below the code above throws a "ReferenceError: "nextPageToken" is not defined. (line 13, file "Code")". Now I'm stuck again. Can you help me make this work?

Community
  • 1
  • 1
Ted Parker
  • 93
  • 7

1 Answers1

4

the nextPageToken I referred to in the previous answer is a property of the first query's results.

to best cope with an undetermined maximum number of users in your example you'll best do this by wrapping the query to the AdminSDK in a while loop that terminates as soon as there are no more users to find:

function writeToSpreadsheet(){
  var values = [],
      users = [],
      userListQuery = {},
      nextPageToken = '',
      listObject = {
          domain:'klht.org',
          maxResults: 500,
          query: "orgUnitPath=/CleverTeachers/King"
      },
      i = 0,
      spreadsheetID = '1JLDD2wm0_udmTn9ZHvdKhL_Ok3SvKYFqkBeiA1GdnYc';

  do {

    if (nextPageToken && nextPageToken !== '') {
      listObject.pageToken = nextPageToken;
    }  

    userListQuery = AdminDirectory.Users.list(listObject);

    // if there are more users than fit in the query a nextPageToken is returned
    nextPageToken = userListQuery.nextPageToken;

    // Add the query results to the users array
    users = users.concat(userListQuery.users);

  } while (nextPageToken);

  for (i = 0; i < users.length; i += 1) {
    values.push([users[i].name.fullName, users[i].primaryEmail]);   
  }

  SpreadsheetApp.openById(spreadsheetID).getSheets()[0].getRange(1, 1, values.length, values[0].length).setValues(values);
}
JSDBroughton
  • 3,966
  • 4
  • 32
  • 52