0

I create a script to get all the users in one SpreadSheet, Name and Email, but I'm not able to get the users that are created under the secondary domain.

This is my code:

function writeToSpreadsheet(){
  var values = [],
  users = [],
  userListQuery = {},
  nextPageToken = '',
  listObject = {
      domain:'the domain name',
      maxResults: 500,        
  },
  i = 0,
  activeSpreadsheet;
 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.getActiveSpreadsheet().getSheets()[0].getRange(2, 1, values.length, values[0].length).setValues(values);
}
Charlie_
  • 303
  • 1
  • 3
  • 7

1 Answers1

0

Use the Google Admin Settings API, it allows administrators of Google Apps domains to retrieve and change settings of their domains in the form of Google Data API feeds. To retrieve from secondary domain, you may send HTTP GET to the account information administrator secondary email address feed URL and include an Authorization header as described in Authenticating to the Admin Settings service A successful response returns an HTTP 200 OK, along with the administrator's secondary email address.

Sample HTTP GET request: https://apps-apis.google.com/a/feeds/domain/2.0/{domainName}/accountInformation/adminSecondaryEmai

Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30