Microsoft Graph Directory Schema Extensions are a convenient way to store additional data on certain objects such as users or groups. You can read about them here. This blog post is assuming you have already registered an extension and now you’re looking to be able to retrieve the extension and values for a user ( I will use a user object as an example ) or update the value using the Microsoft Graph .Net SDK. To make changes on a user object, you will need the permission “User.ReadWrite.All”. My sample project is in a console application. You can find the full project on my GitHub here. The project is using the client credentials grant flow.

.Net Code to get an Extension Attribute Value:

        static async Task<string> GetExtensionAttributeValue(string upn, string extensionName)
        {
            User u = await GraphServiceClient.Users[upn]
                .Request()
                .Select($"id,{extensionName}")
                .GetAsync();

            try
            {
                // the additional data field on the user object has the extension attributes
                return u.AdditionalData[extensionName].ToString(); 
            } catch
            {
                // if the property isn't found, a null exception key not found type of error occurs, just return an empty string
                return string.Empty;
            }
        }

Code to Update ( or Add if not already on the object ) an Extension value:

        static async Task<List<string>> UpdateOrAddExtensionAttributeValue(string upn, string extensionName, string value)
        {
            List<string> status = new List<string>();

            User u = await GraphServiceClient.Users[upn]
                .Request()
                .Select($"id,displayName,{extensionName}")
                .GetAsync();

            if (!u.AdditionalData.ContainsKey(extensionName))
            {
                // the extension wasn't on the user so we are going to add it to the AdditionalData collection before saving
                u.AdditionalData.Add(extensionName, value);
                status.Add($"Added the value for '{u.AdditionalData[extensionName]}' to the user '{upn}'.");
            } else
            {
                // set the new value on the user object in the Additional Data Field
                u.AdditionalData[extensionName] = value; 
                status.Add($"Changed the value for '{extensionName}' to '{u.AdditionalData[extensionName]}' for user '{upn}'");
            }

            // save the change on the user object
            try
            {
                await GraphServiceClient.Users[u.Id].Request().UpdateAsync(u);
                status.Add($"\nSuccessfully updated the user '{upn}'");
            } catch (ServiceException e)
            {
                status.Add($"MS Graph error: {e.Error}");
            }
            return status;
        }

If you try to update an extension that does not exist for your tenant, you will get this type of error:

‘Code: Request_BadRequest
Message: The following extension properties are not available: extension_ab1bceabeaa348b79744917b2ffae7e1_primaryContact.
Inner error:
AdditionalData:
date: 2022-05-09T18:38:20
request-id: 7ebc8f76-dac2-40b9-8ee4-9b33522a04b1
client-request-id: 7ebc8f76-dac2-40b9-8ee4-9b33522a04b1
ClientRequestId: 7ebc8f76-dac2-40b9-8ee4-9b33522a04b1′

Summary

Working with extension attributes with the Microsoft Graph .Net SDK is fairly straightforward. The same process would be true for the other objects you have extension attributes on such as groups.

0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Moumita Chatterjee
Moumita Chatterjee
August 25, 2022 3:14 pm

getting error “Unsupported Schema Extension Property value of type String”. What can be reason?