learntogrow-growtolearn
learntogrow-growtolearn

Reputation: 1280

Access Microsoft.Graph.User user principal name property - Graph API

I am using Microsoft Graph v1 API to get the list of all the users in a group. Something like :

var groupInfo = await graphServiceClient.Groups[group].Members.Request()
                                        .GetAsync().ConfigureAwait(false);

This returns me the list of <Microsoft.Graph.User> objects. The only accessible property in this object seems to be Id, OData and AdditionalData. Is there any way I can fetch properties like "UserPrincipalName" , "DisplayName" ?

I don't wish to hit graph API again to get these values based on the user ID that I am able to access.

I tried using:

var groupInfo = await graphServiceClient.Groups[group].Members.Request().Select("userPrincipalName")
                                        .GetAsync().ConfigureAwait(false);

However, this still seems to give the User object and not just UPN. Is this a bug somewhere?

Upvotes: 1

Views: 1560

Answers (1)

Darrel Miller
Darrel Miller

Reputation: 142014

You need to upcast the DirectoryObject into it's native type to be able to access it.

var groupInfo = await graphServiceClient.Groups[group].Members.Request().Select("userPrincipalName")
                                        .GetAsync().ConfigureAwait(false);

var user = groupInfo[0] as User;

Upvotes: 3

Related Questions