Schema Extension Value is always NULL when updating through Microsoft Graph SDK

Viewed 210

Step 1:

Created GraphServiceClient using Microsoft.Graph 4.9.0 and Microsoft.Graph.Core 2.0.5 SDK

var scopes = new[] { "https://graph.microsoft.com/.default" };
ClientSecretCredential clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, new ClientSecretCredentialOptions()
{
     AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
});`


GraphServiceClient graphServiceClient = new GraphServiceClient(clientSecretCredential, scopes);

Step 2:

And created a custom schema extension like below.

SchemaExtension schemaExtension = new SchemaExtension()
{
     Id = "data1",
     Description = "creating test schema extn",
     TargetTypes = new List<string>()
     {
         "User"
     },
     Properties = new List<ExtensionSchemaProperty>()
     {
         new ExtensionSchemaProperty()
         {
              Name ="prop1",
              Type ="String"
         }
     }
};

Step 3:

Updated the Schema extension status to "Available"

var updatedExtn = await graphServiceClient
             .SchemaExtensions[schemaExtension.Id].Request()
             .UpdateAsync(new SchemaExtension()
             {
                 Status = "Available"
             });

Step 4:

Create Class for extension data

public class data1
{
    // You must serialize your property names to camelCase if your SchemaExtension describes as such.
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "prop1", Required = Newtonsoft.Json.Required.Default)]
    public string Prop1 { get; set; }
}

Step 5:

Find the User and add the created schema extension to the user

IDictionary<string, object> extensionInstance = new Dictionary<string, object>();
// The below line is not working. but doesn't throw error
extensionInstance.Add(schemaExtension.Id, new data1 { prop1 = "testing" });

var usrCollection = await graphServiceClient.Users
           .Request()
           .Filter($"userPrincipalNames  eq '{adelev_Mail}'")
           .GetAsync();
 
var usr = usrCollection.FirstOrDefault();

if(usr != null)
{
     usr.AdditionalData.Add(extensionInstance);

     var updatedUser = await graphServiceClient.Users[usr.Id]
               .Request()
               .UpdateAsync(usr);
}

Step 6:

When you try to retrieve the extension the value is NULL.

User updatedUser = await graphServiceClient.Users[usr.Id].Request()
                .Select($"id, {schemaExtension.Id}")
                .GetAsync();

But it works with API using Graph Explorer.

PATCH https://graph.microsoft.com/v1.0/users/{userId}
{
    "extXXXXXXXX_data1":
     {
          "prop1" : "testing"
     }
}

Please let me know if I'm missing anything here. Any help here is much appreciated.

1 Answers

You should accessing the data on AdditionalData property. Try looking at user.AdditionalData in your result. Here is a screenshot with my example.

Getting User with Schema extension from Graph explorer. enter image description here

While using the SDK, i access my custom data in user.AdditionalData

enter image description here

Check this thread - Graph SDK and SchemaExtensions for details.

Related