5

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.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Akhil K J
  • 121
  • 8
  • 1
    hi Akhil can you include What you have tried so far in your question? – ArunPratap Oct 08 '18 at 07:23
  • 2
    @ArunPratap Sorry for the late response. I have added my code in the question. – Akhil K J Oct 08 '18 at 08:58
  • What scopes have your requested? You need to have `User.ReadWrite` in order to make changes to the current User. – Marc LaFleur Oct 08 '18 at 13:29
  • @MarcLaFleur Yes. I tried using User.ReadWrite and User.ReadWrite.All. But no luck. What I actually need is the correct syntax of adding an open extension to the graph. I couldn't find that anywhere. – Akhil K J Oct 09 '18 at 07:45
  • If you're getting an access denied, it is unlikely a syntax issue. It means you don't have permission to execute the action. – Marc LaFleur Oct 09 '18 at 21:11

2 Answers2

6

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();
Akhil K J
  • 121
  • 8
1
GraphServiceClient client = AuthHelper.GetAuthClient();

var currentUser = await client.Me.Request().GetAsync();
string id = currentUser.Id;

var test = await client.Me.Extensions.Request().GetAsync();

Console.WriteLine(currentUser.DisplayName);
Console.WriteLine("Click to update extension");
Console.ReadLine();

Dictionary<string, object> addData = new Dictionary<string, object>()
{
    {"Key", "Value"}
};

var extObject = new OpenTypeExtension()
{
    ExtensionName = "testExtension",
    AdditionalData = addData
};

try
{
    await client.Me.Extensions.Request().AddAsync(extObject);
}
catch (Exception ex)
{
    //
}
pushkin
  • 9,575
  • 15
  • 51
  • 95
alexH
  • 11
  • 2
  • This does not provide an answer to the question. You can [search for similar questions](//stackoverflow.com/search), or refer to the related and linked questions on the right-hand side of the page to find an answer. If you have a related but different question, [ask a new question](//stackoverflow.com/questions/ask), and include a link to this one to help provide context. See: [Ask questions, get answers, no distractions](//stackoverflow.com/tour) – Zoe Nov 09 '18 at 13:11
  • yes i tried to somehow delete the answer, i actually wanted to comment - but there seems to be no obv way to delete a post - so, what can i do – alexH Nov 09 '18 at 13:12
  • If I remember correctly, unregistered users can't delete their own posts. Also, you need 50 reputation to comment. – Zoe Nov 09 '18 at 13:14