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?