mathsbeauty
mathsbeauty

Reputation: 366

Retrieve custom attribute from user profile in Google API Scripts- Google Admin Directory

This is about G suite users.The following works in Google Admin Directory using Google Admin SDK. It retrieves email address and full name of user.

var myemail = Session.getActiveUser().getEmail();

  var mycontact = AdminDirectory.Users.get(myemail);
  var myname = mycontact.name.fullName;

There is a custom attribute in user profile named "Department". The following does NOT retrieve anything. It throws null

var mydept = mycontact.Department;

How can one retrieve custom attribute from user profile in G suite?

Upvotes: 2

Views: 5790

Answers (3)

Chris
Chris

Reputation: 2107

According to Directory Api - Users: get you need to set the projection to "custom".

projection - What subset of fields to fetch for this user.

Acceptable values are:

"basic": Do not include any custom fields for the user. (default)

"custom": Include custom fields from schemas requested in customFieldMask.

"full": Include all fields associated with this user.

Then you should define a Schema for the custom data

customFieldMask (string) A comma-separated list of schema names. All fields from these schemas are fetched. This should only be set when projection=custom.

So something like:

var mycontact = AdminDirectory.Users.get({
  "userKey": myemail,
  "projection": "full",
  "customFieldMask": "Define Schema Here"
});

You can then Logger.log(mycontact); to see how to access the returned custom fields

Upvotes: 6

Romain
Romain

Reputation: 1

If you got an error :

Resource Not Found: userKey

Try this :

mycontact = AdminDirectory.Users.get(
  myemail,{
  projection: 'full'
});

Upvotes: -1

George Helyar
George Helyar

Reputation: 5288

For a custom schema, you can just use the full projection to get all custom schema fields.

For the standard department field, see user.organizations[0].department

https://developers.google.com/admin-sdk/directory/v1/reference/users

Upvotes: 0

Related Questions