Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add extensions using Microsoft Graph Client?

I want to add extensions to a User using GraphClient. I couldn't find the proper C# code anywhere. Can anyone help?

Dictionary<string, object> addData = new Dictionary<string, object> {
    {
    "HoloFlag",
    currentUser.UserPrincipalName
    }
};

var extPatchObject = new OpenTypeExtension();

extPatchObject.ExtensionName = "com.holobeam3.extension";
extPatchObject.AdditionalData = addData;

try {
    var extension = await _graphClient
        .Me
        .Extensions
        .Request()
        .AddAsync(extPatchObject);
    Debug.Log(extension);
} catch (Exception ex) {
    Debug.Log(ex.Message);
}

This is what I tried so far. This returns an "access denied" exception but there is no issue in accessing existing extensions or other Me endpoints of the User.

like image 546
Akhil K J Avatar asked Mar 05 '23 06:03

Akhil K J


1 Answers

In order to add a new extension follow this code:

 _graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(async requestMessage =>
            {
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
            }));
Dictionary<string, object> addData = new Dictionary<string, object>
                {
                    {"Key","Value" }
                };
                var extObject = new OpenTypeExtension();
                extObject.ExtensionName = "TestExtension";
                extObject.AdditionalData = addData;
                try
                {
                    await _graphClient.Users["usernamegoeshere"].Extensions.Request().AddAsync(extObject);
                }

In order to fetch an extension follow this code:

var userExtensions = await _graphClient.Me.Extensions["nameofExtension"].Request().GetAsync();
like image 163
Akhil K J Avatar answered Mar 16 '23 01:03

Akhil K J