首页 > 解决方案 > 如何使用 Microsoft Graph .NET SDK 创建和获取 Outlook 事件打开扩展数据

问题描述

虽然 MSDN 有一些关于如何创建和获取开放扩展的很好的文档,但我找不到使用 Microsoft Graph SDK 用于相同目的的任何东西。

所以我一直在尝试以下。

使用新的开放类型扩展更新事件:

await new EventRequest(fullEventUrl graphClient, null)
.UpdateAsync(new Event
{
    // Change the subject so that I can tell the event is updated by looking at my calendar
    Subject = "Updated Event " + Guid.NewGuid(),
    // Add a new open type extension.
    Extensions = new EventExtensionsCollectionPage
    {
        // I also don't know how to add my own properties to the extension.
        // Tried using my own derived class here but didn't work either.
        new OpenTypeExtension {ExtensionName = "com.consoto.customExtensionName"}
    }
});

此调用为我提供了包含事件详细信息的成功响应,但是,返回的事件 JSON 中没有扩展。似乎表明该事件是在忽略我放在那里的扩展名的情况下创建的。

使用扩展扩展过滤器获取事件:

await new EventRequest(fullEventUrl, graphClient, null).Expand(
"Extensions($filter=id eq 'com.consoto.customExtensionName')").GetAsync();

这成功获取了事件,JSON 中有一个空的扩展集合。

我是否在这里遗漏了什么,或者 SDK 仍然没有更新以支持4 年后创建开放扩展?

标签: microsoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-calendar

解决方案


我从这个 Stack Overflow 问题中找到了答案:Patch Microsoft.Graph Event does not add new Extension

长话短说,我想做的是一个“补丁”,以更新现有事件。

我无法在补丁中添加扩展,因为扩展与我正在修补的事件是不同的实体。

扩展必须按照问题建议单独添加,您可以通过多种方式使用 SDK 执行此操作:

var extension = new OpenTypeExtension {ExtensionName = "MyExtensionName"};

// Method 1 if you don't have the event URL:
graphClient.Users[user].Events[eventId].Extensions.Request().AddAsync(extension);

// Method 2 if you have the event URL:
var extensionCollectionUrl = "https://graph.microsoft.com/v1.0/Users/24c.../Events/AQM.../Extensions";
new OpenTypeExtensionRequest(extensionCollectionUrl, graphClient, null).CreateAsync(extension);

// Method 2 other alternatives:
new EventExtensionsCollectionRequest(extensionCollectionUrl, graphClient, null).AddAsync(extension);

new EventExtensionsCollectionRequestBuilder(extensionsUrl, graphClient).Request().AddAsync(extension);

new OpenTypeExtensionRequestBuilder(extensionsUrl, graphClient).Request().CreateAsync(extension);

至于如何添加“ExtensionName”以外的键值对,可以添加为“AdditionalData”:

var extension = new OpenTypeExtension
{
    ExtensionName = extensionName,
    AdditionalData = new Dictionary<string, object>
    {
        {"testKey", "testValue"}
    }
};

但是,不支持在附加数据上过滤事件(或任何其他类型的资源),因此这仅在附加数据不用于查找相应事件时才有用。


推荐阅读