Admin SDK Directory Service

The Admin SDK Directory service allows you to use the Admin SDK's Directory API in Apps Script. This API gives administrators of Google Apps domains (including resellers) the ability to manage devices, groups, users, and other entities in their domains.

Reference

For detailed information on this service, see the reference documentation for the Admin SDK Directory API. Like all advanced services in Apps Script, the Admin SDK Directory service uses the same objects, methods, and parameters as the public API.

Sample code

The sample code below uses version 1 of the API.

List all users

This sample lists all the users in a domain sorted by first name.

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);
}

Get user

This sample gets a user by their email address and logs all of their data as a JSON string.

function getUser() {
  var userEmail = 'liz@example.com';
  var user = AdminDirectory.Users.get(userEmail);
  Logger.log('User data:\n %s', JSON.stringify(user, null, 2));
}

Add user

This sample adds a new user to the domain, including only the required information. For the full list of user fields, see the API's reference documentation.

function addUser() {
  var user = {
    primaryEmail: 'liz@example.com',
    name: {
      givenName: 'Elizabeth',
      familyName: 'Smith'
    },
    // Generate a random password string.
    password: Math.random().toString(36)
  };
  user = AdminDirectory.Users.insert(user);
  Logger.log('User %s created with ID %s.', user.primaryEmail, user.id);
}

Create alias

This sample creates an alias (nickname) for a user.

function createAlias() {
  var userEmail = 'liz@example.com';
  var alias = {
    alias: 'chica@example.com'
  };
  alias = AdminDirectory.Users.Aliases.insert(alias, userEmail);
  Logger.log('Created alias %s for user %s.', alias.alias, userEmail);
}

List all groups

This sample lists all the groups in the domain.

function listAllGroups() {
  var pageToken, page;
  do {
    page = AdminDirectory.Groups.list({
      domain: 'example.com',
      maxResults: 100,
      pageToken: pageToken
    });
    var groups = page.groups;
    if (groups) {
      for (var i = 0; i < groups.length; i++) {
        var group = groups[i];
        Logger.log('%s (%s)', group.name, group.email);
      }
    } else {
      Logger.log('No groups found.');
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
}

Add group member

This sample adds a user to an existing group in the domain.

function addGroupMember() {
  var userEmail = 'liz@example.com';
  var groupEmail = 'bookclub@example.com';
  var member = {
    email: userEmail,
    role: 'MEMBER'
  };
  member = AdminDirectory.Members.insert(member, groupEmail);
  Logger.log('User %s added as a member of group %s.', userEmail, groupEmail);
}

Send feedback about...

Apps Script
Apps Script