2

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.

Steve
  • 7,747
  • 5
  • 31
  • 44

1 Answers1

0

I'm trying to accomplish the same, just via PostMan for now, and also fail to use patch. I suspect that excerpt from the docs is not saying quite what it appears to but rather that once the extension exists we can change it through patch.

I have succeeded with creating an extension on an existing event as follows, informed by https://stackoverflow.com/a/38471514, by rather using a POST on the extensions collection.

POST https://graph.microsoft.com/v1.0/users/<upn>/calendar/events/<event-id>/extensions

{
  "@odata.type": "microsoft.graph.openTypeExtension",
  "extensionName": "Com.<company-name>.PlacesAvailable",
  "placesAvailable": "50"
}
Martin D
  • 160
  • 1
  • 12
  • What you have posted is the work-around I am doing. The SDK code above at the end of my post does exactly this. Specifically this line: graphServiceClient.Users[].Events[].Extensions.Request().AddAsync(extension); However - I'd like to do the add during the PATCH of the event. – Steve Mar 03 '20 at 03:35
  • You can't use PATCH as the extension is actually a separate object being created and associated/attached to the existing event, thus POST is required – GavinB Jun 19 '20 at 17:23