user3024922
user3024922

Reputation: 56

How can I get the user's display name?

I'm using the G Suite Admin SDK users/list functionality to get a list of users in my domain. This works except that the returned information does not include the user's display name. When I say the "display name" I mean the name that is shown in Gmail or Calendar, which is sometimes different from what's available in the "name" field in the user object, which is only familyName, fullName, and givenName.

How can I get the display name?

It isn't clear how this related to a user's Profile and/or Google+ Domain Profile. There are other APIs related to those, but none of them seem to return what I'm looking for.

I'm actually doing this in a Google Apps Script and the relevant code is:

        var allUsers = [];
        var pageToken;
        var page;
        do {
            page = AdminDirectory.Users.list({
                domain: domain,
                orderBy: 'givenName',
                maxResults: 100,
                pageToken: pageToken,
                viewType: 'domain_public'
            });
            var users = page.users || [];
            for (var i = 0; i < users.length; i++) {
                allUsers.push(users[i]);
            }
            pageToken = page.nextPageToken;
        } while (pageToken);
        return allUsers;

Upvotes: 0

Views: 343

Answers (1)

Mario R.
Mario R.

Reputation: 679

For your code you can try to use page.users[i].name.givenName. Replacing users[i] for allUsers.push(page.users[i].name.givenName). Run your code and you will get the idea in how to retrive that specific information from the user, you will see code completion after you add the "." to pages.users[i].

  var allUsers = [];
        var pageToken;
        var page;
        do {
            page = AdminDirectory.Users.list({
                domain: domain,
                orderBy: 'givenName',
                maxResults: 100,
                pageToken: pageToken,
                viewType: 'domain_public'
            });
            var users = page.users || [];
            for (var i = 0; i < users.length; i++) {
                allUsers.push(page.users[i].name.givenName);
            }
            pageToken = page.nextPageToken;
        } while (pageToken);
        return (console.log(allUsers));

Upvotes: 0

Related Questions