代码之家  ›  专栏  ›  技术社区  ›  Chartreugz

如何设置Microsoft.Exchange.WebServices.Data。属性更新()时的约会?

  •  4
  • Chartreugz  · 技术社区  · 8 年前

    问题

    我有一个带有Microsoft.Exchange的Visual Studio 2015控制台应用程序。安装了WebServices v2.2.0 NuGet包。我正在尝试创建约会、更新约会并取消约会,同时在日历邀请中自动生成的“When”字符串中保留正确的时区 身体 当前,初始创建具有正确的时区,但任何后续更新都会导致时区恢复为UTC。

    注意:我们有一台Exchange 2010服务器,使用Outlook 2013客户端。

    代码示例

    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();
            }
        }
    }
    

    以上代码的结果

    初始邀请(正确时区)

    Initial Invite

    更新的约会(正文中的时区不正确)

    Body Updated

    预约取消(正文中的时区不正确)

    Appointment Cancelled

    提供的代码的控制台结果

    Console Result

    我在找什么

    我不需要在邀请函正文中附加这个额外的“何时”(上图中用红色下划线)。要么我想完全删除它(首选),要么我想在任何更新中更正它。

    1 回复  |  直到 8 年前
        1
  •  3
  •   Chartreugz    7 年前

    问题似乎是EWS 2.2.0 DLL中的一个错误,时区SOAP头未添加到Update()和CancelMeeting()Exchange事务中。下面的代码通过手动附加正确的头来解决此问题。

    对于Update():

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

    对于取消会议():

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

    活动实施:

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