2

I borrowed code from here to download directory data from my Google domain into a spreadsheet. All I changed in the code was to replace OpenByURL with OpenByID because of an invalid argument error. Now it works, except it only returns the first 100 users from my domain. I have about 1000 users total, perhaps a few more. Can I pull all of them? If not, can I at least increase the limit beyond 100? And/or how can I limit my query to a particular Organizational Unit?

My full code below:

function writeToSpreadsheet(){
  var values = [];
  var users = AdminDirectory.Users.list({domain:'klht.org'}).users; //example: ignitesynergy.com
  for (var i=0; i<users.length; i++){
    values.push([users[i].name.fullName, users[i].primaryEmail]); //Look in the docs or use auto complete to see what you can access  
  }
  var spreadsheetID = '1JLDD2wm0_udmTn9ZHvdKhL_Ok3SvKYFqkBeiA1GdnYc';
  SpreadsheetApp.openById(spreadsheetID).getSheets()[0].getRange(1, 1, values.length, values[0].length).setValues(values);
}

//Red indicates the places you need to use your info
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Ted Parker
  • 93
  • 7

1 Answers1

3

The maximum you can pull down is 500 – maxResults

Also add the orgUnitPath as part of a query field in your list call:

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

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

 var usersPage2 = AdminDirectory.Users.list({
                                      domain: 'klht.org',
                                      maxResults: 500,
                                      query: "orgUnitPath=/OU",
                                      pageToken: nextPageToken
                                      }).users;
JSDBroughton
  • 3,966
  • 4
  • 32
  • 52
  • Thank you! I've gotten the OU to work. Can you help me make the nextpagetoken trick work? Simply pasting it below my code from above resulted in "ReferenceError: "nextPageToken" is not defined. (line 13, file "Code")". ' – Ted Parker Dec 21 '15 at 22:58
  • 1
    @TedParker - if you have a new question, please do make it a new question. Refer back to this one if it helps set context. – Mogsdad Dec 22 '15 at 04:41
  • nextPageToken is a property of the first query. if you do not directly assign the `.users` property to `var users` you can access it. `AdminDirectory.Users.list({ domain: 'klht.org', maxResults: 500, query: "orgUnitPath=/OU" }).pageToken;` – JSDBroughton Dec 22 '15 at 15:33
  • see the fuller answer to your other question to see how to recursively check for all users in the stack. http://stackoverflow.com/a/34419185/1100265 – JSDBroughton Dec 22 '15 at 15:34