0

In a Google Workspace for Education environment I want to work with a customSchema in the user's profile in order to automate some administrative tasks.

Suppose I have made a customSchema called "housing" in the admin backend. Within "housing" I have made an item "rooms", which contains the value "4".

I succeeded already in retrieving all the user's profile information. The output (Logger.log...) indeed contains the customSchema. Retrieving a fullName or a givenName from the profile is no problem either.

This is my working code so far.

function myTryout() {
  // code needs import of service = Admin SDK
  var myEmailAdress = Session.getActiveUser().getEmail();
  // INFO = https://stackoverflow.com/questions/48912906/retrieve-custom-attribute-from-user-profile-in-google-api-scripts-google-admin 
  var iAm = AdminDirectory.Users.get(myEmailAdress);
  var myName = iAm.name.fullName;
  Logger.log("USER'S FULL NAME = " + myName);
  allMyData = AdminDirectory.Users.get(myEmailAdress,{projection: 'full'});
  Logger.log("ALL USER'S DATA = " + allMyData);
}

However, accessing the data from customSchemas doesn't seem as straightforward.

So what is the code to extract the value for "rooms" from a user's data? And what is the code to write (or overwrite) this value from a user's data?

Logger.log("ALL USER'S DATA = " + allMyData) shows me all available data in the user's profile:

{"thumbnailPhotoEtag":"\"3mhkESrZKxFccxpwig0yxvrUnxZYMxZUQujDNbVwMy4/jqzjWbJaD1m-N5bWVFSSTQ59Rr8\"","primaryEmail":"a_teacher@babotaniek.be","id":"112697199011841056502","emails":[{"address":"a_teacher@babotaniek.be","primary":true},{"address":"a_teacher@babotaniek.be.test-google-a.com"}],"orgUnitPath":"/","customerId":"C04agxt6k","agreedToTerms":true,"isEnrolledIn2Sv":false,"isMailboxSetup":true,"name":{"givenName":"a","familyName":"teacher","fullName":"a teacher"},"kind":"admin#directory#user","isDelegatedAdmin":false,"isAdmin":false,"etag":"\"3mhkESrZKxFccxpwig0yxvrUnxZYMxZUQujDNbVwMy4/oVtc4zE6rVvNCi4C8ycs-g15wqw\"","archived":false,"creationTime":"2018-09-06T20:19:55.000Z","ipWhitelisted":false,"changePasswordAtNextLogin":false,"isEnforcedIn2Sv":false,"lastLoginTime":"2022-02-15T11:27:05.000Z","nonEditableAliases":["a_teacher@babotaniek.be.test-google-a.com"],"customSchemas":{"babotaniek":{"aantal_km_met_de_fiets_heen_en_terug":6,"rijksregisternummer":"76798732544","stamboeknummer":"67867547567865","rekeningnummer":"FR798757654877658"},"housing":{"rooms":6,"sinks":7}},"languages":[{"preference":"preferred","languageCode":"nl"}],"thumbnailPhotoUrl":"https://www.google.com/s2/photos/private/AIbEiAIAAABECPa96Lmo5-CasAEiC3ZjYXJkX3Bob3RvKig4YjcxMDRhNzBiNmZkNDRiM2E3MmZjNzk3OTAzNGY0NTkyMTM2MmVhMAEsJu9fD0gMXnysdJsU_77BAHnQgQ","includeInGlobalAddressList":true,"suspended":false}

Note that the customSchemas and the values therein are listed.

If you're so kind to answer my question, please keep in mind that I'm rather new to coding. I didn't understand the few things I found on the web, so please give an answer suitable for dummies.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Can you share a sample of your code ? – liquidkat Feb 16 '22 at 17:13
  • I have added the code that works so far. I get all the user's data with it but cannot single out the values in the customSchema in my domain. – Dirk Ploegaerts Feb 16 '22 at 18:41
  • Reference :https://developers.google.com/admin-sdk/directory/reference/rest/v1/users You should be able to get customSchemas from allMyData return(if this throw no errors). Use update methods to overwrite the existing data. – liquidkat Feb 16 '22 at 19:28
  • what's the response you get in `allMyData` – David Salomon Feb 16 '22 at 20:01
  • This is the response I get in allMyData when I use it with a dummy account. Note that the customSchemas and the values therein are listed > shorturl.at/nyzI4 – Dirk Ploegaerts Feb 16 '22 at 21:23

2 Answers2

0

I believe you only want to retrieve the data within a specific Custom Schema, in this scenario you can make use of the standard resource Fields where you can specify which value you want returned. So if you are looking to only retrieve the specific custom schema data you can use this code:

function myTryout() {
  // code needs import of service = Admin SDK
  var myEmailAdress = Session.getActiveUser().getEmail();
  // INFO = https://stackoverflow.com/questions/48912906/retrieve-custom-attribute-from-user-profile-in-google-api-scripts-google-admin 
  var iAm = AdminDirectory.Users.get(myEmailAdress);
  var myName = iAm.name.fullName;
  Logger.log("USER'S FULL NAME = " + myName);
  allMyData = AdminDirectory.Users.get(myEmailAdress,{fields: 'customSchemas',projection: 'Custom',customFieldMask: 'rooms'});
  Logger.log("Custom SChema data = " + allMyData);
}

I modified your code a bit so it will display only the information of one specific Custom Schema, if you want to retrieve all the values in all Custom Schemas, change projection to Full and delete the CustomFieldMask as this searches through specific schemas. You can check it out and test it using the Users.get first so you can have an idea.

Now if what you want is to overwrite the data within the customSchema you can make use of users.update following the same logic by modifying the Fields resource.

Rubén
  • 34,714
  • 9
  • 70
  • 166
Gabriel Carballo
  • 1,278
  • 1
  • 3
  • 9
  • Either I don't get it or there is something wrong with the code. The line allMyData = ... gives me the following error: GoogleJsonResponseException: API call to directory.users.get failed with error: Invalid Input: [rooms] – Dirk Ploegaerts Feb 17 '22 at 12:25
  • Still I think we're not far from the solution. – Dirk Ploegaerts Feb 17 '22 at 12:28
  • [rooms] was just an example, the customFieldMask is the value of the customSchema that you want to retrieve, so if the customSchema data that you want to retrieve is called `AWS` then that's the name that you should use instead. In your case `housing` should return values added in rooms. – Gabriel Carballo Feb 17 '22 at 15:26
  • We're almost there. – Dirk Ploegaerts Feb 17 '22 at 21:35
  • Almost there. AdminDirectory.Users.get(myEmailAdress,{fields: 'customSchemas',projection: 'Custom',customFieldMask: 'housing'}); gives me this: {"customSchemas":{"housing":{"sinks":3,"rooms":4}}} Now I still don't get how to retrieve the value "4" from "rooms". Can you point out this last step for me? (Sorry to be a nuissance. I'm a total newbie in this.) – Dirk Ploegaerts Feb 17 '22 at 21:47
0

The solution to my problem appeared to be very straightforward.

function thisWorks() {
// code needs: import of service = Admin SDK
// code needs predefined customSchema 'housing' with item 'rooms'
var myEmailAdress = Session.getActiveUser().getEmail(); //e-mailaddress of current user
var iAmAll = AdminDirectory.Users.get(myEmailAdress,{projection: 'full'}); // ALL USER DATA of current user, including customSchemas
var testCustom = iAmAll.customSchemas.housing.rooms;
Logger.log("TESTING = " + testCustom);
}