Exchange Web Services (EWS) – Part 1

So, few months ago I worked on a project where integration with exchange e-mails and calendar was needed so someone mentioned EWS. And here I am. Compelled to write a blog post about it.

So, few months ago I worked on a project where integration with exchange e-mails and calendar was needed so someone mentioned EWS. And here I am. Compelled to write a blog post about it.

Few things to clarify first. Coding was done in NAV2016 and e-mail account is hosted on office365.

After the structure of the whole process was designed, I ended up with rather simple flowchart. One of its components being the authorization part. Obviously, following msdn article about EWS as I never used it before, I started with this: https://msdn.microsoft.com/en-us/library/office/ff597939(v=exchg.80).aspx

Initially code looked something like:

ExchangeWebServicesPart1-Picture1

If you compare it with msdn article, it’s more or else the same, apart language differences. And look at me using AutodiscoverUrl next to just setting Url property as mentioned as a recommended way of doing it.

But running this will give following error:

A call to Microsoft.Exchange.WebServices.Data.ExchangeService.AutodiscoverUrl failed with this message: Autodiscover blocked a potentially insecure redirection to https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml. To allow Autodiscover to follow the redirection, use the AutodiscoverUrl(string, AutodiscoverRedirectionUrlValidationCallback) overload.

Serves me right for trying to be lazy.

Let me fix that quick. What is AutodiscoverRedirectionUrlValidationCallback you ask (or not if you’re experienced EWS developer)?  No idea, but msdn knows: https://msdn.microsoft.com/en-us/library/office/microsoft.exchange.webservices.autodiscover.autodiscoverredirectionurlvalidationcallback(v=exchg.80).aspx

If you’ve been following vjekos blog on http://vjeko.com/ (great site by the way :)) there is a post about limitations of .NET in NAV 2013 (http://vjeko.com/top-10-things-i-miss-in-net-interoperability-in-nav-2013/ ) and at place #1 is Delegates. Yes, they are still a problem in NAV2016.

So, the only way to work around this, I need some sort of wrapper class that will use .NET to work with delegates magic instead of NAV code worrying about it. Luckily there is one coming with NAV installation:

ExchangeWebServicesPart1-Picture4

Looking at its methods, it seems I don’t have to worry about redirection and I can instantiate it immediately with credentials. But it requires special type called ExchangeCredentials (not WebCredentials used originally).

Looking at the ExchangeCredential class there is a method called op_Implicit which transforms NetworkCredentials into ExchangeCredentials. Or read here for precise terminology: https://msdn.microsoft.com/en-us/library/dd635572(v=exchg.80).aspx. Just what I needed. I hope.

ExchangeWebServicesPart1-Picture5

Since this wrapper class can return entire instance as an Exchange Service, I don’t need to instantiate it separately so whole thing can work like this:

ExchangeWebServicesPart1-Picture6

And it does. Or does it?

Stay tuned for more EWS goodies on part 2.