如何使用Microsoft Graph Client添加扩展名?

时间:2018-10-08 07:15:16

标签: c# microsoft-graph microsoft-graph-sdks

我想使用GraphClient向用户添加扩展。我在任何地方都找不到合适的C#代码。有人可以帮忙吗?

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

这是我到目前为止尝试过的。这将返回“访问被拒绝”异常,但是访问用户的现有扩展或其他Me端点没有问题。

3 个答案:

答案 0 :(得分:3)

要添加新的扩展名,请遵循以下代码:

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

为了获取扩展名,请遵循以下代码:

var userExtensions = await _graphClient.Me.Extensions["nameofExtension"].Request().GetAsync();

答案 1 :(得分:1)

我添加了正确的代码

Dictionary<string, object> addData = new Dictionary<string, object>();

addData.Add("testUpdateKey", "dataUpdateValue");

var extPatchObject = new OpenTypeExtension();

extPatchObject.ExtensionName = "TestData";

extPatchObject.AdditionalData = addData;

try

{
    graphClient.Users["usernameGoesHere"].Extensions["TestData"].Request().UpdateAsync(extPatchObject).Wait();

}

答案 2 :(得分:0)

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)
{
    //
}