I am trying to patch an existing Microsoft Graph Calendar Event object in order to add a new Extension to the Event object. According to the docs this should be possible:
Because the event resource supports extensions, you can use the PATCH operation to add, update, or delete your own app-specific data in custom properties of an extension in an existing event instance. More Info
Here is the Update method where I'm trying to add the extension to an existing Event:
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);
}
The value of extension on the returned event is always null
-- so it seems to not be adding the next extension. Even on subsequent calls to get all events the extension object is null
.
I have other code creating an extension object on a new event, and that is working correctly.
In Summary, I'd like to know how to add a new extension to an existing calendar event on a patch?
Notes
I checked out this SO Question - but he's doing an Add whereas I'm doing an UpdateAsync.
I have a work around
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);
}
I call the above when I want to add extension data to an existing event.