Google Directory Script

Google Directory Script

For additional context on automatic host upload, see this knowledge article: https://success.tractionguest.com/s/article/How-do-I-have-my-host-list-update-automatically

This script will ask for access to Google Drive and your Directory.
Requirements:

  • Admin Directory API must be enabled

  • Google Drive folder to save payload


Below is a template Google Script to automatically upload hosts to your Traction Guest account:

/** * This Script will pull Users from Google Directory and upload them to Traction Guest */ function listUsers() { var users = [], userCSV = 'email,first_name,last_name\n', nextPageToken = '', ListObject = { customer: 'my_customer', orderBy: 'email', maxResults: 500 }; do { //Continue loop if there is a value for the nextPageToken if (nextPageToken && nextPageToken !== '') { ListObject.pageToken = nextPageToken; } var response = AdminDirectory.Users.list(ListObject); //if there are more users than fit in the query a nextPageToken is returned nextPageToken = response.nextPageToken; //Add new page of users to existing list of users var users = users.concat(response.users); } while (nextPageToken); //Loop through Users and pull fields for (i = 0; i < users.length; i++) { userCSV += users[i].primaryEmail + ','; userCSV += users[i].name.givenName + ','; userCSV += users[i].name.familyName + '\n'; } //Log within Scripts Logger.log(userCSV); //OPTIONAL - Save CSV to specific Drive folder //Folder ID pulled from URL of folder var driveFolder = DriveApp.getFolderById('[GOOGLE DRIVE FOLDER ID]'); var file = driveFolder.createFile('Hosts - ' + new Date, userCSV, MimeType.CSV) //Upload to Traction Guest var url = 'https://us.tractionguest.com/people/import_v2?remove_unmatched_hosts=true&ignore_ownership=true&overwrite_person_groups=true&remove_empty_groups=true'; var options = { 'method': 'POST', 'headers': { //Token for upload is generated on Preferences Page 'Authorization': 'Basic [HOST UPLOAD TOKEN]'}, //Payload is the same as file saved to GoogleDrive 'payload': file}; var response = UrlFetchApp.fetch(url, options); Logger.log(response); }