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

    Posted on June 6, 2013 by in C#, Dotnet

    In this article, I am going to explain how to cancel an appointment using EWS web services.

    If 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 EWS

    I have dowloaded the EWS API assembly (Microsoft.Exchange.WebServices) from the following location.
    http://www.microsoft.com/en-us/download/details.aspx?id=28952

    What is Exchange Service Object?
    The ExchangeService object manages the connection between Exchange Web Services (EWS) and Client application application. It provides properties and methods that authenticate your application account holders to EWS, methods that enable you to work with batches of Exchange items at the same time.

    Example:
    I have created a method CancelAppoinment which would take user’s mail box address, appointment ID & cancel meeting text as arguments. I have created a local variable ExchangeService. In CancelAppoinment method, 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.  First thing we need to do is to inform the exchange service about the user mail box we are interested in. This we do by setting the ImpersonatorID property of the user’s email address.

    I have fetched the appointment object based on the appointment event item ID. Once we have the appointment object, we simply need to call the CancelMeeting method to cancel the meeting. The meeting will be removed from the organizer’s mailbox and a cancel notification will be send to all participants.

    using System;
    using System.Configuration;
    using System.Net;
    using Microsoft.Exchange.WebServices.Data;
    
    namespace TipsConsoleApp_2
    {
    public class ExchangeHelper
    {
        
        /// <summary>
        /// method to cancel a meeting
        /// </summary>
        /// <param name="mailBox">target user's email address</param>
        /// <param name="appointmentID">appointment event id</param>
        /// <param name="cancelBody">cancel meeting text</param>
        public void CancelAppointment(string mailBox, string appointmentID, string cancelBody)
        {
            ExchangeService 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"]
                                                );       
    
            //Set the ImpersonatedUserId property of the ExchangeService object to identify 
            //the impersonated user (target account).        
            exchangeService.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, mailBox);
    
            //get the appointment based on the appointment ID
            Appointment appointment = Appointment.Bind(exchangeService, new ItemId(appointmentID));
    
            //cancel the appointment
            appointment.CancelMeeting(cancelBody);
                   
            //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;
        }    
    }
    }
    

    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.CancelAppointment("[email protected]",                "AAMkADk5MDlmMmVjLTdiMzktNDE3Yi04YjJlLWUwZWUzZmRkYzVlOABGAAAAAAAQRgn9Cy0FR7sYF+7WPI9cBwCD87KcPW/eQajoNLU7q3ojAAAAADr2AABXtDIfzg+pQ71mVI3AgHdOAAAAA9pOAAA=",
                    "have conflicting meeting at the sametime");
    }
    

    Output:
    I have created a new meeting in outlook client. Test User 200 is the organizer and Test User 100 is the required participant.
    cancel appointment 1

    After I run the console application, meeting is cancelled and following cancel notification is sent to Test User 100.
    cancel appointment 2

    Be Sociable, Share!

    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]

    Leave a Reply