【发布时间】:2020-02-25 18:04:16
【问题描述】:
我正在尝试修补现有的 Microsoft Graph 日历 Event 对象,以便将新的 Extension 添加到 Event 对象。根据文档,这应该是可能的:
因为事件资源支持扩展,所以可以使用 PATCH 添加、更新或删除您自己的应用程序特定数据的操作 现有事件中扩展的自定义属性 实例。 More Info
这是我尝试将扩展添加到现有事件的更新方法:
public async Task<Event> UpdateEventAsync(string userId, string eventId, JsonPatchDocument<EventForUpdate> patchedEvent)
{
// Create a new event for updating
var newEvent = new Event();
// Get the Extension data from the patch object: IDictionary<string,object>
var extensionData = GetExtensionData(patchedEvent, newEvent);
// Create new extension and add to event object
var extension = new OpenTypeExtension()
{
ODataType = "microsoft.graph.openTypeExtension",
ExtensionName = EXTENSION_NAME,
AdditionalData = extensionData,
Id = "Microsoft.OutlookServices.OpenTypeExtension." + EXTENSION_NAME
};
newEvent.Extensions = new EventExtensionsCollectionPage() { extension };
// Create the request - unsure if Expand is necessary.
var request = graphServiceClient.Users[userId].Events[eventId].Request();
var reqExpanded = request.Expand($"extensions($filter=id%20eq%20'Microsoft.OutlookServices.OpenTypeExtension.{EXTENSION_NAME}')");
return await reqExpanded.UpdateAsync(newEvent);
}
返回事件的扩展值总是null——所以它似乎没有添加下一个扩展。即使在后续调用以获取所有事件时,扩展对象也是null。
我有其他代码在新事件上创建扩展对象,并且工作正常。
总之,我想知道如何为补丁上的现有日历事件添加新的扩展?
注意事项
我查看了this SO Question - 但他正在执行 Add 而我正在执行 UpdateAsync。
我有一个解决办法
private async Task<Extension> AddExtension(string user, string eventId, IDictionary<string, object> metadata)
{
Extension result = null;
// Create new extension and add to event object
var extension = new OpenTypeExtension()
{
ODataType = "microsoft.graph.openTypeExtension",
ExtensionName = EXTENSION_NAME,
AdditionalData = metadata,
};
return await graphServiceClient.Users[user].Events[eventId].Extensions.Request().AddAsync(extension);
}
当我想将扩展数据添加到现有事件时,我会调用上述方法。
【问题讨论】:
标签: c# outlook microsoft-graph-api microsoft-graph-sdks microsoft-graph-calendar