Search code examples
microsoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-calendar

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


While MSDN has some good documentation on how to create and get open extensions, I couldn't find anything for the same purpose using the Microsoft Graph SDK.

So I have been trying the following.

Updating an event with a new open type extension:

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"}
    }
});

This call gives me a successful response with the event details, however, there's no extensions in the returned event JSON. Seems to be an indication that the event is created ignoring the extension I put in there.

Getting the event with expand extension filter:

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

This gets the event successfully, with an empty extension collection in the JSON.

Am I missing something here or the SDK is still not updated to support creating open extension after 4 years?


Solution

  • I found the answer from this Stack Overflow question: Patch Microsoft.Graph Event does not add new Extension.

    Long story short, what I wanted to do was a "patch", to update an existing event.

    I can't add extension in a patch because the extension is a different entity from the event I'm patching.

    The extension has to be added separately as the question suggested, there are multiple ways you can do this using the 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);
    
    

    As for how to add key value pairs other than just "ExtensionName", you can add them as "AdditionalData":

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

    However, filtering events (or any other type of resources) on additional data is not supported, so this is only useful if the additional data is not meant for finding the corresponding event.