如何使用 Microsoft Graph .NET SDK 创建和获取 Outlook 事件打开扩展数据

How to create and get Outlook event open extension data using Microsoft Graph .NET SDK

虽然 MSDN 有一些 good documentation on how to create and get open extensions,但我无法使用 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 仍未更新以支持创建开放扩展 after 4 years

我从这个 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"}
    }
};

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