Reputation: 45
I'm looping through the user list to pull out some data.
Logger.log('\nName : %s\nEmail : %s\nOU : %s\nDepartment : %s\n', users[i].name.fullName, users[i].primaryEmail, users[i].orgUnitPath, users[i].orgDepartment);
Name, email and OU return the expected results but not Department.
Some user accounts have department data and others don't but in any case the result always comes back 'undefined'. What am I doing wrong ?
Full code here.
function listUsers(){
var values = [],
users = [],
userListQuery = {},
nextPageToken = '',
listObject = {
customer: 'my_customer',
query: "isSuspended=false",
maxResults: 500
},
i = 0,
j = 0;
do {
if (nextPageToken && nextPageToken !== '') {
listObject.pageToken = nextPageToken;
}
var userListQuery = AdminDirectory.Users.list(listObject);
// Si plus de maxResults retourner à nextPageToken
nextPageToken = userListQuery.nextPageToken;
// Ajout de résultats à la liste
users = users.concat(userListQuery.users);
} while (nextPageToken);
for (i = 0; i < users.length; i++) {
// Filtrer les résultats
if (users[i].orgUnitPath == '/') {
j++;
Logger.log('Résultat #%s', j.toString());
Logger.log('\nNom : %s\nCourriel : %s\nOU : %s\nDépartement : %s\n', users[i].name.fullName, users[i].primaryEmail, users[i].orgUnitPath, users[i].orgDepartment);
}
}
}
Upvotes: 1
Views: 265
Reputation: 10345
Problem
Users
resource doesn't have top-level property orgDepartment
. Although you can search users using orgDepartment
(as well as address
or other org
-prefixed fields), it is not defined in the returned object (hence the undefined
value).
Solution
Access the top-level property organizations
, which is an Array<Object>
(organizations this user is a part of). Its elements are structured as follows (obviously, you need the department
property):
{
"name": string,
"title": string,
"primary": boolean,
"type": string,
"customType": string,
"department": string,
"symbol": string,
"location": string,
"description": string,
"domain": string,
"costCenter": string,
"fullTimeEquivalent": integer
}
Assuming there is only one organization associated with the user, referencing department should look like: users[i].organizations[0].department
Reference
Upvotes: 2