Azure AD GraphServiceClient Extensions not working

2019-08-16 17:28发布

问题:

I have the following query to retrieve a User using the GraphServiceClient with .net core 2.

 user = await _graphClient.Users[principalName].Request()
                    .Expand("Extensions")
                    .GetAsync();

When I run this I get the following error

Microsoft.Graph.ServiceException: Code: generalException Message: Unexpected exception returned from the service.

This only happens when I have added a OpenTypeExtension to the user using the following code!

extension = new OpenTypeExtension
                {
                    ExtensionName = AzureADExtensions.UserConstants.ExtensionName,
                    AdditionalData = new Dictionary<string, object>
                    {
                        {"OtherEmail", externalUser.Email},
                        {"OtherRole" , externalUser.Roles.FirstOrDefault()}
                    }
                };

                await _graphClient.Users[user.Id].Extensions.Request()
                    .AddAsync(extension);

I am starting to get really getting fed up with Azure AD now.

I can't seem to add any meta data to my users. Only doing this because otherEmails does not work with the GraphServiceClient and trying to use any other sensible fields gives me this error:

Tenant does not have a SPO license when updating user

Any help would be appreciated.

回答1:

So for anyone else that come across this, it seems to be an a bug in the service client.

The code below fails

  user = await _graphClient
                    .Users[userId]
                    .Request()
                    .Expand("Extensions")
                    .GetAsync();

But this code works, just by requesting the extensions in a separate call!

 user = await _graphClient
                    .Users[userId]
                    .Request()
                    .GetAsync();

                var extensions = await _graphClient.Users[user.Id].Extensions.Request().GetAsync();
                user.Extensions = extensions;

Hope that save's someone the time I lost on this!