Azure AD GraphServiceClient can't set AdditionalData against User
Asked Answered
M

2

7

I am using the GraphServiceClient with .Net Core 2. I am trying to add AdditionalData to my Users using the following code

var updated = new User()
                {
                   AdditionalData = new Dictionary<string, object>
                    {
                        {"OtherEmail", otherEmail},
                        {"OtherRole", otherRole}
                    },
                };

                await _graphClient.Users[user.Id].Request().UpdateAsync(updated);

When this executes I get the following error

Microsoft.Graph.ServiceException : Code: Request_BadRequest Message: One or more property values specified are invalid.

Does anyone know what I am doing wrong ?

Also if anyone can please tell me how I can just save some of my own metadata against a user it would be really appreciated. I have also tried using the extensions, but I am having this problem.

Azure AD GraphServiceClient Extensions not working

Manoff answered 7/12, 2018 at 13:57 Comment(2)
Are you using Microsoft Graph API or Azure AD Graph API to do this?Trabue
I am using the GraphServiceClient in Microsoft.Graph. github.com/microsoftgraph/msgraph-sdk-dotnetManoff
M
2

So I got around this using OpenExtensions, this also caused me a lot of issues, which can be seen here.

Azure AD GraphServiceClient Extensions not working

Trick is to add the extensions like this

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);

And then retrieve them like this.

user = await _graphClient
                    .Users[userId]
                    .Request()
                    .GetAsync();
// Note : you should be able to expand this on original request, but fails for me.
                var extensions = await _graphClient.Users[user.Id].Extensions.Request().GetAsync();
                user.Extensions = extensions;

Hope that helps someone!

Manoff answered 10/12, 2018 at 14:6 Comment(1)
What is AzureADExtensions.UserConstants.ExtensionName ?Allina
U
5

To add attributes to the AdditionalData field you have to define the attribute in Azure AD B2C > User attributes (in the Azure portal). If you for example create a Boolean attribute Dummy, this will create the field extension_{b2c-extensions-app-id}_Dummy where {b2c-extensions-app-id} is the Application ID of the automatically generated b2c-extensions-app viewable by navigating to Azure AD B2C > App registrations (Preview) > b2c-extensions-app with the dashes (-) removed from the ID!

This would allow you to do the following:

var updated = new User()
    {
        AdditionalData = new Dictionary<string, object>
        {
            {"extension_0000000000000000000000000000_Dummy", false}
        },
    };

await _graphClient.Users[user.Id].Request().UpdateAsync(updated);
Unfrequented answered 21/5, 2020 at 2:46 Comment(1)
Perfect answer thanks. I highlighted the the part where it says Remove - Since I was not doing that and tried to make it work for hours... :DLynnett
M
2

So I got around this using OpenExtensions, this also caused me a lot of issues, which can be seen here.

Azure AD GraphServiceClient Extensions not working

Trick is to add the extensions like this

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);

And then retrieve them like this.

user = await _graphClient
                    .Users[userId]
                    .Request()
                    .GetAsync();
// Note : you should be able to expand this on original request, but fails for me.
                var extensions = await _graphClient.Users[user.Id].Extensions.Request().GetAsync();
                user.Extensions = extensions;

Hope that helps someone!

Manoff answered 10/12, 2018 at 14:6 Comment(1)
What is AzureADExtensions.UserConstants.ExtensionName ?Allina

© 2022 - 2024 — McMap. All rights reserved.