'Retrieving values from customSchemas in Google Script
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":"[email protected]","id":"112697199011841056502","emails":[{"address":"[email protected]","primary":true},{"address":"[email protected]"}],"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":["[email protected]"],"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.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rubén |
