使用 Exchange 中的 EWS 在特定时区中创建约会Create appointments in a specific time zone by using EWS in Exchange

了解如何使用 Exchange 中的 EWS 托管 API 或 EWS 在特定时区中创建约会。Learn how to create appointments in specific time zones by using the EWS Managed API or EWS in Exchange.

在 Exchange 日历上创建约会或会议时,用于指定开始和结束时间的时区将保存为约会的创建时区。When an appointment or meeting is created on an Exchange calendar, the time zone used to specify the start and end times is saved as the creation time zone for the appointment. 该时区也用于解释未指定显式时区的日期和时间值,因此,了解用于指定时区的选项非常重要。That time zone is also used to interpret date and time values that do not have an explicit time zone specified, so it is important to understand your options to specify the time zone.

使用 EWS 托管 API 在不同时区创建约会Creating appointments in different time zones by using the EWS Managed API

使用 EWS 托管 API 创建约会或会议时,有三个用于指定时区的选项:When creating appointments or meetings using the EWS Managed API, you have three options for specifying the time zone:

  • 若要使用在其中执行 EWS 托管 API 的计算机的时区,请不要在创建ExchangeService对象时指定时区。To use the time zone of the computer where your EWS Managed API is executing, do not specify a time zone when creating the ExchangeService object.

  • 若要将特定时区用于所有日期/时间属性(包括创建新约会或会议时的属性),请在ExchangeService对象的构造函数中指定一个时区。To use a specific time zone for all date/time properties, including properties when creating a new appointment or meeting, specify a time zone in the constructor for the ExchangeService object.

  • 若要使用与ExchangeService属性中指定的时区不同的时区,请使用StartTimeZoneEndTimeZone属性。To use a different time zone than the one specified in the ExchangeService.TimeZone property, use the Appointment.StartTimeZone and Appointment.EndTimeZone properties.

    备注

    仅当ExchangeService属性设置为Exchange2010或更高版本时, EndTimeZone属性才可用。The EndTimeZone property is only available when the ExchangeService.RequestedServerVersion property is set to Exchange2010 or later. 如果不可用,则设置StartTimeZone将应用于约会的开始时间和结束时间。If it is not available, setting the StartTimeZone applies to both the start and end times of the appointment.

在下面的示例中,将使用 EWS 托管 API 创建三个约会。In the following example, the EWS Managed API is used to create three appointments. 每个约会都设置为从现在起两天的时间(未指定的时区)1:00 开始,并在稍后结束一个小时。Each appointment is set to start at 1:00 PM two days from now, in an unspecified time zone, and end one hour later. 在客户端计算机的时区中,使用默认的 EWS 托管 API 行为创建第一个约会。The first appointment is created in the client computer's time zone by using default EWS Managed API behavior. 使用StartTimeZoneEndTimeZone属性在中央时区中创建第二个。The second is created in the Central time zone by using the Appointment.StartTimeZone and Appointment.EndTimeZone properties. 第三个是在山地时区中使用ExchangeService属性创建的。The third is created in the Mountain time zone by using the ExchangeService.TimeZone property.

using Microsoft.Exchange.WebServices.Data;
using System.Security;
static void CreateAppointments(string userEmail, SecureString userPass)
{
    // *****************************************************
    // Create an appointment using the client computer's time zone.
    // *****************************************************
    // Do not specify a time zone when creating the ExchangeService.
    ExchangeService clientTZService = new ExchangeService(ExchangeVersion.Exchange2010);
    clientTZService.Credentials = new NetworkCredential(userEmail, userPass);
    clientTZService.AutodiscoverUrl(userEmail, redirectionCallback);
    // Create the appointment.
    Appointment clientTZAppt = new Appointment(clientTZService);
    clientTZAppt.Subject = "Appointment created using client time zone";
    clientTZAppt.Body = new MessageBody(string.Format("Time zone: {0}", clientTZService.TimeZone.DisplayName));
    // Set the start time to 1:00 PM two days from today.
    DateTime startTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 2);
    startTime = startTime.AddHours(13);
    clientTZAppt.Start = startTime;
    // Set the end time to 2:00 PM on that same day.
    DateTime endTime = startTime.AddHours(1);
    clientTZAppt.End = endTime;
    // Save the appointment to the default calendar.
    try
    {
        // This method results in a call to EWS.
        clientTZAppt.Save(SendInvitationsMode.SendToNone);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error saving appointment: {0}", ex.Message);
    }
    // *****************************************************
    // Create an appointment in the Central time zone by
    // using the StartTimeZone property.
    // *****************************************************
    // Retrieve the Central time zone.
    TimeZoneInfo centralTZ = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    // Create the appointment.
    Appointment centralTZAppt = new Appointment(clientTZService);
    centralTZAppt.Subject = "Appointment created using Central time zone";
    centralTZAppt.Body = new MessageBody(string.Format("Time zone: {0}", centralTZ.DisplayName));
    // Set the time zone on the appointment.
    centralTZAppt.StartTimeZone = centralTZ;
    centralTZAppt.EndTimeZone = centralTZ;
    // Set the start time to 1:00 PM two days from today.
    centralTZAppt.Start = startTime;
    // Set the end time to 2:00 PM on that same day.
    centralTZAppt.End = endTime;
    // Save the appointment to the default calendar.
    try
    {
        // This method results in a call to EWS.
        centralTZAppt.Save(SendInvitationsMode.SendToNone);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error saving appointment: {0}", ex.Message);
    }
    // *****************************************************
    // Create an appointment in the Mountain time zone by
    // using the ExchangeService.TimeZone property.
    // *****************************************************
    // Specify the Mountain time zone when creating the ExchangeService.
    TimeZoneInfo mountainTZ = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");
    ExchangeService mountainTZService = new ExchangeService(ExchangeVersion.Exchange2010, mountainTZ);
    mountainTZService.Credentials = new NetworkCredential(userEmail, userPass);
    mountainTZService.AutodiscoverUrl(userEmail, redirectionCallback);
    // Create the appointment.
    Appointment mountainTZAppt = new Appointment(mountainTZService);
    mountainTZAppt.Subject = "Appointment created using Mountain time zone";
    mountainTZAppt.Body = new MessageBody(string.Format("Time zone: {0}", mountainTZService.TimeZone.DisplayName));
    // Set the start time to 1:00 PM two days from today.
    mountainTZAppt.Start = startTime;
    // Set the end time to 2:00 PM on that same day.
    mountainTZAppt.End = endTime;
    // Save the appointment to the default calendar.
    try
    {
        // This method results in a call to EWS.
        mountainTZAppt.Save(SendInvitationsMode.SendToNone);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error saving appointment: {0}", ex.Message);
    }
}

在东部时区配置的客户端计算机上执行此示例时,将从在东部时区中配置的客户端查看这三个约会,它们分别显示为 1:00 PM、2:00 PM 和 3:00 PM。When this example is executed on a client computer configured in the Eastern time zone, and the three appointments it creates are viewed from a client configured in the Eastern time zone, they appear at 1:00 PM, 2:00 PM, and 3:00 PM, respectively.

使用 EWS 在不同时区创建约会Creating appointments in different time zones by using EWS

使用 EWS 创建约会或会议时,有三个用于指定时区的选项:When creating appointments or meetings using EWS, you have three options for specifying the time zone:

下面的示例CreateItem 操作请求使用 UTC 创建约会。The following example CreateItem operation request creates an appointment using UTC. 请注意, TimeZoneContext元素、 StartTimeZone元素和EndTimeZone元素不存在。Notice that the TimeZoneContext element, the StartTimeZone element, and the EndTimeZone element are absent. "开始" 和 "结束" 元素值以 UTC 表示。The Start and End element values are expressed in UTC.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" 
    xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types" 
    xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2010" />
  </soap:Header>
  <soap:Body>
    <m:CreateItem SendMeetingInvitations="SendToNone">
      <m:Items>
        <t:CalendarItem>
          <t:Subject>Appointment created using UTC</t:Subject>
          <t:Body BodyType="HTML">Time zone: UTC</t:Body>
          <t:Start>2014-06-07T17:00:00.000Z</t:Start>
          <t:End>2014-06-07T18:00:00.000Z</t:End>
        </t:CalendarItem>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

下面的示例CreateItem 操作请求使用StartTimeZoneEndTimeZone元素指定约会的中央时区。The following example CreateItem operation request uses the StartTimeZone and EndTimeZone elements to specify the Central time zone for the appointment. 请注意, TimeZoneContext元素不存在。Notice that the TimeZoneContext element is absent. 但是,如果它存在,则StartTimeZoneEndTimeZone元素的值将覆盖其值。However, if it were present, the values of the StartTimeZone and EndTimeZone elements would override its value. 同样,开始结束元素值以 UTC 表示。Again, the Start and End element values are expressed in UTC.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" 
    xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types" 
    xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2010" />
  </soap:Header>
  <soap:Body>
    <m:CreateItem SendMeetingInvitations="SendToNone">
      <m:Items>
        <t:CalendarItem>
          <t:Subject>Appointment created using Central time zone</t:Subject>
          <t:Body BodyType="HTML">Time zone: (UTC-06:00) Central Time (US &amp;amp; Canada)</t:Body>
          <t:Start>2014-06-07T18:00:00.000Z</t:Start>
          <t:End>2014-06-07T19:00:00.000Z</t:End>
          <t:StartTimeZone Id="Central Standard Time" />
          <t:EndTimeZone Id="Central Standard Time" />
        </t:CalendarItem>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

下面的示例CreateItem 操作请求将TimeZoneContext元素设置为山区时区。The following example CreateItem operation request sets the TimeZoneContext element to the Mountain time zone. 请注意," StartTimeZone " 和 " EndTimeZone " 元素不存在。Notice that the StartTimeZone and EndTimeZone elements are absent. 同样,开始结束元素值以 UTC 表示。Again, the Start and End element values are expressed in UTC.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" 
    xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types" 
    xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <t:RequestServerVersion Version="Exchange2010" />
    <t:TimeZoneContext>
      <t:TimeZoneDefinition Id="Mountain Standard Time" />
    </t:TimeZoneContext>
  </soap:Header>
  <soap:Body>
    <m:CreateItem SendMeetingInvitations="SendToNone">
      <m:Items>
        <t:CalendarItem>
          <t:Subject>Appointment created using Mountain time zone</t:Subject>
          <t:Body BodyType="HTML">Time zone: (UTC-07:00) Mountain Time (US &amp;amp; Canada)</t:Body>
          <t:Start>2014-06-07T19:00:00.000Z</t:Start>
          <t:End>2014-06-07T20:00:00.000Z</t:End>
        </t:CalendarItem>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

当由在东部时区配置的客户端查看前 EWS 示例创建的三个约会时,它们分别显示在 1:00 PM、2:00 PM 和 3:00 PM 处。When the three appointments created by the previous EWS example requests are viewed from a client configured in the Eastern time zone, they appear at 1:00 PM, 2:00 PM, and 3:00 PM, respectively.

现在,您已了解如何在特定时区中创建约会,您可能需要将现有约会上的时区更新到不同的约会。Now that you know how to create appointments in specific time zones, you might want to update the time zones on existing appointments to a different one.

另请参阅See also