Search code examples
c#sqlmodel-view-controlleroutlookactive-directory

How to automatically update data from outlook to sql using c#, When someone rejects my meeting request it should be updated to my database


When someone accepts or rejects my meeting request in outlook. It should be updated automaticaaly to my database as updated or rejected. should be done in c#.

I am trying to use active directory but its not working


Solution

  • To be informed when something changes within an outlook calendar you must be actively informed by Microsoft about this change. This can be accomplished by registering a webhook. This means you register a URL via Graph that should be called when a change happens and this URL must be public available.

    This theme is quite complex, but if you want to walk that path, you should take a look at the documentation.

    Unfortunately it isn't easy to provide a simple code sample that demonstrates the usage. You have to setup a web server (maybe ASP.NET Core) that is public available. This one needs a self-defined route that can by called anonymously. Then you have to follow the documentation to let Microsoft call your endpoint when something changed.

    The hardest problems I had, when setting it up, was that Microsoft sends for subscription validation plain text, which is not supported by default in ASP.NET Core. To add a TextPlainInputFormatter I took this implementation. And in case of subscription validation the ASP model validation must be skipped, cause the body is empty.

    Also be aware, while this endpoint is public available and doesn't support any normal authentication (like Bearer token or basic) you can still validate that this is a valid request, cause on subscription you define a clientState. This can be any random string (we chose Guid.NewGuid()). If you store it on your site you can compare any incoming request against this secret value for validation.