I am using a sample script to create a user but it keeps get this error ReferenceError: "AdminDirectory" is not defined
/**
* 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:
* @see https://developers.google.com/admin-sdk/directory/v1/reference/users/insert
*/
function addUser() {
var user = {
primaryEmail: '[email protected]',
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);
}
You need to enable the API in 2 places.


For those using the new interface for Apps Script, normally you would add a service in the editor itself (Files, Libraries, Services):

Unfortunately the AdminDirectory service does not seem to be available in the new interface (as at July 2022). For the next few months while the classic editor is still available, you can click "Use classic editor".
From there it's as Amit explained, Resources menu → Advanced Google services... then turn on AdminDirectory next to Admin SDK API:

Once you've added the service you can go back to the new interface to continue editing and it will show up in the expected spot:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With