Search code examples
c#exchangewebservicesexchange-server-2010outlook-2013

How to set Microsoft.Exchange.WebServices.Data.Appointment When property on Update()?


The Problem

I have a Visual Studios 2015 Console Application with the Microsoft.Exchange.WebServices v2.2.0 NuGet package installed. I'm trying to create an appointment, update it, and cancel it while retaining the correct Timezone in the "When" string generated automatically in the calendar invite body. Currently, the initial creation has the correct timezone, but any subsequent updates cause the timezone to revert to UTC.

Note: We have an Exchange 2010 server and use Outlook 2013 clients.

Code Sample

using System;
using System.Globalization;
using Microsoft.Exchange.WebServices.Data;

namespace EWSTesting
{
    class Program
    {
        private const string EmailServer = ""; //replace with your Exchange server
        private const string EmailAddress = ""; //replace with your email

        static void Main(string[] args)
        {
            Console.WriteLine("Current Timezone: " + TimeZoneInfo.Local.DisplayName);

            var exchangeService = new ExchangeService(ExchangeVersion.Exchange2010, TimeZoneInfo.Local)
            {
                PreferredCulture = new CultureInfo("en-US"),
                Url = new Uri(EmailServer),
                UseDefaultCredentials = true
            };

            Console.WriteLine("exchangeService.TimeZone.DisplayName: " + exchangeService.TimeZone.DisplayName);

            var startDate = DateTime.Today;
            var endDate = startDate.AddHours(1);

            //Create initial appointment
            var appointment = new Appointment(exchangeService)
            {
                Subject = "Testing Appointments",
                Body = "Testing Appointments Body",
                Location = "Test Location",
                LegacyFreeBusyStatus = LegacyFreeBusyStatus.Busy,
                Sensitivity = Sensitivity.Private,
                Start = startDate,
                End = endDate
            };
            appointment.OptionalAttendees.Add(EmailAddress);
            appointment.Save(SendInvitationsMode.SendOnlyToAll);

            Console.WriteLine("exchangeService.TimeZone.DisplayName: " + exchangeService.TimeZone.DisplayName);

            var appointmentId = appointment.Id;

            Console.WriteLine("Pause to check inbox 'When' value on invite");
            Console.ReadLine();

            appointment = Appointment.Bind(exchangeService, appointmentId);

            appointment.Load(new PropertySet(PropertySet.FirstClassProperties)
            {
                AppointmentSchema.StartTimeZone,
                AppointmentSchema.EndTimeZone,
                AppointmentSchema.TimeZone
            });

            appointment.Body = "Body Updated Successfully";
            appointment.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendOnlyToAll);

            Console.WriteLine("exchangeService.TimeZone.DisplayName: " + exchangeService.TimeZone.DisplayName);
            Console.WriteLine("appointment.StartTimeZone.DisplayName: " + appointment.StartTimeZone.DisplayName);
            Console.WriteLine("appointment.EndTimeZone.DisplayName: " + appointment.EndTimeZone.DisplayName);
            Console.WriteLine("appointment.TimeZone: " + appointment.TimeZone);

            Console.WriteLine();
            Console.WriteLine("Pause to check updated inbox 'When' value on invite");
            Console.ReadLine();

            appointment = Appointment.Bind(exchangeService, appointmentId);

            appointment.Load(new PropertySet(PropertySet.FirstClassProperties)
            {
                AppointmentSchema.StartTimeZone,
                AppointmentSchema.EndTimeZone,
                AppointmentSchema.TimeZone
            });

            Console.WriteLine("exchangeService.TimeZone.DisplayName: " + exchangeService.TimeZone.DisplayName);
            Console.WriteLine("appointment.StartTimeZone.DisplayName: " + appointment.StartTimeZone.DisplayName);
            Console.WriteLine("appointment.EndTimeZone.DisplayName: " + appointment.EndTimeZone.DisplayName);
            Console.WriteLine("appointment.TimeZone: " + appointment.TimeZone);

            appointment.CancelMeeting();

            Console.WriteLine("Appointment Deleted");
            Console.ReadLine();
        }
    }
}

The Results of the above code

Initial Invite (Correct Timezone)

Initial Invite

Updated Appointment (Incorrect Timezone in body)

Body Updated

Appointment Cancellation (Incorrect Timezone in body)

Appointment Cancelled

Console Result of code provided

Console Result

What I'm Looking For

I do not need this additional "When" (underlined in red in pictures above) to be appended to the body of the invite. Either I would like to completely remove it (preferred) or I would like to correct it in any updates.


Solution

  • It appears that the issue is a bug in the EWS 2.2.0 DLL, the timezone SOAP headers are not being added to the Update() and CancelMeeting() Exchange transactions. The code below resolves this issue by manually appending the correct header.

    For Update():

    exchangeService.OnSerializeCustomSoapHeaders += service_OnSerializeCustomSoapHeaders;
    appointment.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendOnlyToAll);
    exchangeService.OnSerializeCustomSoapHeaders -= service_OnSerializeCustomSoapHeaders;
    

    For CancelMeeting():

    exchangeService.OnSerializeCustomSoapHeaders += service_OnSerializeCustomSoapHeaders;
    appointment.CancelMeeting();
    exchangeService.OnSerializeCustomSoapHeaders -= service_OnSerializeCustomSoapHeaders;
    

    Event Implementation:

    static void service_OnSerializeCustomSoapHeaders(XmlWriter writer)
    {
        writer.WriteRaw(Environment.NewLine + "    <t:TimeZoneContext><t:TimeZoneDefinition Id=\"" + TimeZoneInfo.Local.StandardName + "\"/></t:TimeZoneContext>" + Environment.NewLine);
    }