• How to create appointment using EWS (Exchange Web Services)

    Posted on May 28, 2013 by in C#, Dotnet

    In this example, I am going to create a new appointment using exchange web services (2007 & 2010).

    What is EWS (Exchange Web Services)?
    Exchange web services enable applications to communicate with exchange server.  All the functionality that is exposed through outlook client can be done using web services.

    How do I access EWS?
    It is same as accessing any web service. If you are using a .NET client, you could add web reference by calling https://{server_name}/ews/exchange.asmx. This would create the proxy class for the application to use.

    (or)

    You could directly download the EWS API assembly (Microsoft.Exchange.WebServices) from the following location and add it as reference to the application.
    http://www.microsoft.com/en-us/download/details.aspx?id=28952

    Exchange Helper Class
    I have created a helper class that would act as a wrapper around EWS functionality. I have created a class variable ExchangeService. In the constructor, I am initializing ExchangeService object.  I am getting the service url from the appsettings and setting it to the object.  Then I am setting the credentials I use to for accessing the service.

    I have created a public method CreateAppoinment which would create an appointment on user’s calendar for the specified time.  We need to specify the target account to the service object by setting the ImpersonatorID property of the service to the user’s email address. After we do that, we create a new appointment object and bind it to the service. We set some properties and finally call SAVE method of the appointment object to create appointment.

    using System;
    using System.Configuration;
    using System.Net;
    using Microsoft.Exchange.WebServices.Data;
    
    namespace TipsConsoleApp_2
    {
    public class ExchangeHelper
    {
        ExchangeService exchangeService;
    
        public ExchangeHelper()
        {
            //Instantiate a new ExchangeService object
            exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
    
            //Set the exchange WebService URL
            exchangeService.Url = new Uri(ConfigurationSettings.AppSettings["EXCHANGE_SVC_URL"]);
    
            //Set the credentials of the service to the credentials 
            //that are associated with the impersonating account.
            exchangeService.Credentials = new NetworkCredential(                                            
                                            ConfigurationSettings.AppSettings["EXCHANGE_LOGIN_NAME"],
                                            ConfigurationSettings.AppSettings["EXCHANGE_PASSWORD"],
                                            ConfigurationSettings.AppSettings["EXCHANGE_HOST_NAME"]
                                                );            
    
        }
    
        public string CreateAppointment(string mailBox, string recipients,
                            string body, string subject, DateTime startTimeUTC, DateTime endTimeUTC)
        {
            //Set the ImpersonatedUserId property of the ExchangeService object to identify the impersonated user (target account). 
            //This example uses the user's SMTP email address.
            exchangeService.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, mailBox);
    
            //create a new appointment object
            Appointment appointment = new Appointment(exchangeService);
    
            //set appointment properties
            appointment.Subject = subject;
            appointment.Body = body;
    
            //In MSDN it says that if you dont specify the timezone, it will use the UTC timezone
            //but in reality it is not working that way.
            //so explicity setting the EST timezone
            appointment.StartTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
            appointment.Start = startTimeUTC.ToLocalTime();
            appointment.EndTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
            appointment.End = endTimeUTC.ToLocalTime();
    
            //add required participants
            appointment.RequiredAttendees.Add(recipients);
    
            //send meeting request to all participants and save a copy in user's sentitems
            appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);
    
            //Set it back to null so that any actions that will be taken using the exchange service
            //applies to impersonating account (i.e.account used in network credentials)
            exchangeService.ImpersonatedUserId = null;
    
            //return the unique identifier that is created
            return appointment.Id.UniqueId;
        }
    }
    }
    

    Console Application
    I have created a simple console application to test this example. I have added following settings to the app.config file.

    <appSettings>
        
        <add key="EXCHANGE_SVC_URL" value="https://{service_url}"/>
        <add key="EXCHANGE_HOST_NAME" value="{domain_name}"/>
        <add key="EXCHANGE_LOGIN_NAME" value="{login_name}"/>
        <add key="EXCHANGE_PASSWORD" value="{password}"/>
      </appSettings>
    

    In the main method, I am simply calling the create appointment method with some test input.

    public static void Main(string[] args)
            {
                ExchangeHelper exchangeHelper = new ExchangeHelper();
                exchangeHelper.CreateAppointment("[email protected]", 
                  "[email protected]", "test body", "test subject", DateTime.UtcNow, DateTime.UtcNow.AddHours(1));
    

    create appointment using EWS 1

    Be Sociable, Share!
      Post Tagged with , , ,

    Written by

    Software architect with over 10 years of proven experience in designing & developing n-tier and web based software applications, for Finance, Telecommunication, Manufacturing, Internet and other Commercial industries. He believes that success depends on one's ability to integrate multiple technologies to solve a simple as well as complicated problem.

    View all articles by

    Email : [email protected]

    One Responseso far.

    1. […] you are new to using EWS, I would recommend you to read below articles. How to create appointment using EWS How to search appointments using […]

    Leave a Reply