'No Sufficient Data Content of User When Calling Microsoft.Graph Compared to Microsoft.Azure.ActiveDirectory.GraphClient
I want to change my code from Microsoft.Azure.ActiveDirectory.GraphClient to Microsoft.Graph on the same registered App. On some data structure(e.g. Group, Company), the fetched data are the same. but the fetched data of User are different. (I am using the V1.0 of the Graph API.)
For example, when I try to fetch certain user by its upn. The old method is to call:
graphClient.Users.Where(user => user.UserPrincipalName.Equals(upn)).ExecuteSingleAsync()
//graphClient is an instance of ActiveDirectoryClient
And the new one is to call:
graphClient.Users.Request().Filter("userPrincipalName eq '"+ upn + "'").GetAsync()
//graphClient is an instance of GraphServiceClient
Although they will both fetch the same user with correct information of input UPN. But compared to the old method, data fetched by new method are not sufficient, many of its properties are null. Such as AccountEnabled, Othermails, AssignedPlans, and UserType.
Am I calling GraphServiceClient in a wrong way so that it cannot fetch all the data? Or should I fetch these null properties with a supplement call?
Thanks!
Solution 1:[1]
Am I calling GraphServiceClient in a wrong way so that it cannot fetch all the data?
That's the normal behaviour of Microsoft Graph user API, see Microsoft Graph user API here and this extract:
By default, only a limited set of properties are returned (
businessPhones, displayName, givenName, id, jobTitle, mail, mobilePhone, officeLocation, preferredLanguage, surname, userPrincipalName
). This example illustrates the default request and response.
Or should I fetch these null properties with a supplement call?
We could use $select parameter to query the property. The following is demo code you could refer to. You could select the properties that you wanted. It works for me.
var user = graphserviceClient.Users.Request().Select(x =>new
{
x.UserPrincipalName,
x.DisplayName,
x.AccountEnabled,
x.UsageLocation
} ).Filter("userPrincipalName eq '" + upn + "'")
.GetAsync().Result;
Test result:
Solution 2:[2]
You can add a Select
statement to your request to include certain fields:
var users = await graphserviceClient.Users.Request()
.Select("displayName,givenName,surname,accountEnabled,mail,mobilePhone,userPrincipalName,assignedPlans")
.GetAsync()
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 | |
Solution 2 | Roger Far |