簡體   English   中英

如何在Update()上設置Microsoft.Exchange.WebServices.Data.Appointment When屬性?

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

問題

我有安裝了Microsoft.Exchange.WebServices v2.2.0 NuGet程序包的Visual Studios 2015控制台應用程序。 我正在嘗試創建約會,對其進行更新並取消它,同時在日歷邀請正文中自動生成的“何時”字符串中保留正確的時區。 當前,初始創建具有正確的時區,但是任何后續更新都會導致時區恢復為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();
        }
    }
}

上面代碼的結果

初始邀請(正確的時區)

初始邀請

更新的約會(正文中的時區不正確)

身體更新

約會取消(正文中的時區不正確)

預約已取消

控制台提供的代碼結果

控制台結果

我在尋找什么

我不需要將此額外的“時間”(在上面的圖片中用紅色下划線標記)附加到邀請的正文中。 我想完全刪除它(首選),或者我想在任何更新中更正它。

看來這是EWS 2.2.0 DLL中的錯誤,時區SOAP標頭未添加到Update()和CancelMeeting()Exchange事務中。 下面的代碼通過手動附加正確的標頭來解決此問題。

對於Update():

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

對於CancelMeeting():

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM