1

I'm trying to use the Google Admin SDK to get all of the users for a directory on our site but I'm trying to figure out what exactly page on line 4 of the code means.

function listAllUsers() {
      var pageToken, page;
      do {
        page = AdminDirectory.Users.list({
          domain: 'example.com',
          orderBy: 'givenName',
          maxResults: 100,
          pageToken: pageToken
        });
        var users = page.users;
        if (users) {
          for (var i = 0; i < users.length; i++) {
            var user = users[i];
            Logger.log('%s (%s)', user.name.fullName, user.primaryEmail);
          }
        } else {
          Logger.log('No users found.');
        }
        pageToken = page.nextPageToken;
      } while (pageToken);
    }
2
  • Did the code work? If results are beyond the maxResults=100, then the next batch is processed: pageToken. – Jacob Jan Tuinstra Feb 3 '14 at 15:16
  • No I'm getting an error on line 4 of the code; "resource not found:domain(line four, file "code")". – Ronan Feb 3 '14 at 15:20
1

There are couple of things you need to do when performing this piece of code.

  1. enable the Admin Directory API in the script editor
    enter image description here
  2. enable the Drive SDK from the Google Developers Console
    enter image description here enter image description here
  3. in the Admin console of your Google Apps domain, under Security, enable API access:
    enter image description here
  4. add proper domain name, ie not example.com

I just tried the following revised code, with my own domain name:

function listAllUsers() {
  var users = AdminDirectory.Users.list({domain: 'jacobjantuinstra.nl'}).users;
  if(users.length != 0) {
    for (var i=0; i<users.length; i++) {
      var user = users[i];
      Logger.log('%s (%s)', user.name.fullName, user.primaryEmail);
    }
  } else {
    Logger.log('No users found.');
  }
}

and the following was shown in the logger:
enter image description here

The do while is meant to restrict each iteration to 100 users. If you have less, or have an execution time of less than 5 minutes, then the above code will suffice.

1
  • Thats great thanks, I got the code to work but it throws up an error for google drive when I published it and embedded it on my site so I have to solve that now! – Ronan Feb 4 '14 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.