Exchange Web Services (EWS) Managed API

Unless you are a seasoned COM junky, the Exchange Web Services (EWS) Managed API will come as a welcome addition to anyone needing to programmatically manage Microsoft Exchange appointments, calendars, messages, or any other folders residing on an exchange server. The key difference with EWS is that it is using purely managed code. EWS itself is simply a wrapper around the EWS protocol, so there’s no need to learn another XML-based interface.

You can download EWS from Microsoft (currently an RC) and begin interacting immediately with any server running Exchange 2010 or Exchange 2007 running SP1 or higher. The web service interaction requires SSL and a valid Network Credential for access. The base service is hosted on exchange via the EWS URL below, where <exchange_server_name> is the host name of your Exchange server.

https://<exchange_server_name>/ews/exchange.asmx

To try out EWS in a test environment, you can download the Exchange 2007 virtual machine. Once the Exchange VM (VHD) is running and you are logged in, you need to configure the network settings to utilize automatic IP and DNS settings instead of the default static ones provided. This will assist in routing network traffic to the new machine. To verify the setup, you can browse the following URL below and see the WSDL for the EWS. You will need to ignore the certificate warning and enter a login with access to the service (login as litwareinc\Administrator)

https://ex07sp1/ews/exchange.asmx

The next step is installing EWS. After installation, you can begin creating Visual Studio projects using the new API. MSDN provides a nice introduction and valuable references for working with the managed API. After creating a new Visual Studio project, you need to add a reference to the new assembly Microsoft.Exchange.Webservices.dll (located at the path Program Files\Microsoft\Exchange\Web Services\1.0\ ). Next, we add using statements for the new library reference and also for X509 certificates.

using Microsoft.Exchange.WebServices.Data;
using System.Security.Cryptography.X509Certificates;

Now here’s some simple code to create a connection to the EWS endpoint. The certificate callback is necessary since we’re using a user-generated X509 certificate.

// Hook up the cert callback.
            ServicePointManager.ServerCertificateValidationCallback =
                delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
                {
                    // Validate the certificate and return true or false as appropriate.
                    // Note that it not a good practice to always return true because not
                    // all certificates should be trusted.
                    return true;
                };

            // assign exchange version
            ExchangeVersion version = ExchangeVersion.Exchange2007_SP1;

            // create service endpoint connection
            ExchangeService service = new ExchangeService(version);

            // Assign NetworkCredential directly
            service.Credentials = new NetworkCredential("username", "password", "domain");

            // EWS network location
            service.Url = new Uri("https://ex07sp1/ews/exchange.asmx");

Now we’ll create an appointment using this service endpoint. Note: When you add attendees, you actual create a meeting instead of an appointment.

            Appointment appt = new Appointment(service);
            appt.Subject = "Read EWS Documentation";
            appt.Body = "You can download the Exchange 2007 SP2 here: <a href='http://www.microsoft.com/downloads/thankyou.aspx?familyId=ee7829a3-0ae8-44de-822c-908cd1034523&displayLang=en'>SP2 Download</a>";
            appt.StartTimeZone = TimeZoneInfo.Local;
            appt.Start = DateTime.Now.AddDays(1);
            appt.ReminderMinutesBeforeStart = 15;
            appt.End = appt.Start.AddHours(2);
            appt.RequiredAttendees.Add("Administrator@litwareinc.com");
            appt.Save(); // persisted to exchange

To probe deeper, you can follow the exchange developer blog on MSDN. We look forward to the RTM of this powerful managed Exchange API.

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Twitter

Filed under: Application Development, Microsoft | Posted on September 22nd, 2009 by AaronH

Tags: , , , , ,

Leave a Reply

Links

Topics

Tags

Authors

Syndication

Archives

Copyright © 2010 Pearl Technology. All rights reserved.
The Tech Blue theme was modified to help create this blog.