Exchange の EWS を使用して、特定のタイム ゾーンで予定を作成する

Exchange の予定表に予定または会議を作成するときに、開始時刻と終了時刻の指定に使用したタイム ゾーンは、予定の作成タイム ゾーンとして保存されます。 そのタイム ゾーンは、明示的にタイム ゾーンが指定されていない日付および時刻の値の解釈にも使用されるため、タイム ゾーンを指定するオプションについて理解することが重要になります。

EWS マネージ API を使用して異なるタイム ゾーンで予定を作成する

EWS マネージ API を使用して予定または会議を作成するときには、タイム ゾーンを指定するための 3 つのオプションがあります。

  • EWS マネージ API を実行しているコンピューターのタイム ゾーン使用する。この場合は、ExchangeService オブジェクトの作成時にタイム ゾーンを指定しません。

  • すべての日付/時刻のプロパティ (新しい予定や会議の作成時のプロパティも含む) に特定のタイム ゾーンを指定する。この場合は、ExchangeService オブジェクトのコンストラクターでタイム ゾーンを指定します。

  • ExchangeService.TimeZone プロパティで指定したものとは別のタイム ゾーンを使用する。この場合は、Appointment.StartTimeZone プロパティと Appointment.EndTimeZone プロパティを使用します。

注:

EndTimeZone プロパティは、ExchangeService.RequestedServerVersion プロパティが Exchange2010 以降に設定されている場合にのみ使用できます。 使用できない場合は、 StartTimeZone を設定すると、予定の開始時刻と終了時刻の両方に適用されます。

次の例では、3 つの予定の作成に EWS マネージ API が使用されています。 それぞれの予定は、未指定のタイム ゾーンで現在から 2 日後の 1:00 PM に始まり、その 1 時間後に終わります。 最初の予定は、既定の EWS マネージ API の動作を使用することで、クライアント コンピューターのタイム ゾーンで作成されます。 2 つ目は 、Appointment.StartTimeZone プロパティと Appointment.EndTimeZone プロパティを使用して中央タイム ゾーンに作成されます。この場合、TimeZoneDescription 拡張プロパティも使用されている TimeZone と同じ値に設定します。 3 番目は、ExchangeService.TimeZone プロパティを使用することで、山地標準時で作成されます。

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.
    // *****************************************************
    // Extended Property for the TimeZone Description
    ExtendedPropertyDefinition tzDescription = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Appointment, 33332, MapiPropertyType.String);
    // 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;
    // Set the TimeZone Description on the appointment/meeting
    centralTZAppt.SetExtendedProperty(tzDescription, centralTZ.DisplayName);
    // 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);
    }
}

注:

2 番目の例では、TimeZoneDescription 拡張プロパティを設定して、会議の更新プログラムが入力対象の受信者に送信されるときに、増強の問題を回避する必要があります。

この例を東部標準時で構成されているクライアント コンピューターで実行して、それによって作成された予定を東部標準時で構成されているクライアントで表示すると、それぞれの予定が 1:00 PM、2:00 PM、および 3:00 PM に始まるように示されます。

EWS を使用して異なるタイム ゾーンで予定を作成する

EWS を使用して予定または会議を作成するときには、タイム ゾーンを指定するための 3 オプションがあります。

次の CreateItem 操作要求の例では、UTC を使用して予定を作成します。 TimeZoneContext 要素、StartTimeZone 要素、EndTimeZone 要素が存在しないことに注意してください。 Start 要素と End 要素の値は UTC で表されます。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" 
    xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" 
    xmlns:soap="http://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>2023-02-07T17:00:00.000Z</t:Start>
          <t:End>2023-02-07T18:00:00.000Z</t:End>
        </t:CalendarItem>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

次の CreateItem 操作要求の例では、StartTimeZone 要素と EndTimeZone 要素を使用して、予定に中部標準時を指定します。 TimeZoneContext 要素が存在しないことに注意してください。 ただし、その要素が存在していたとしても、その値は StartTimeZone 要素と EndTimeZone 要素の値で上書きされます。 この場合も、Start 要素と End 要素の値は UTC で表されます。 また、TimeZoneDescription Extended プロパティを使用している TimeZone と同じ値に設定します。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" 
    xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" 
    xmlns:soap="http://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:ExtendedProperty>
             <t:ExtendedFieldURI DistinguishedPropertySetId="Appointment" PropertyId="33332" PropertyType="String" />
             <t:Value>(UTC-06:00) Central Time (US &amp; Canada)</t:Value>
          </t:ExtendedProperty>
          <t:Start>2023-02-07T18:00:00.000</t:Start>
          <t:End>2023-02-07T19:00:00.000</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 要素を山地標準時に設定します。 StartTimeZone 要素と EndTimeZone要素が存在しないことに注意してください。 この場合も、Start 要素と End 要素の値は UTC で表されます。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" 
    xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" 
    xmlns:soap="http://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>2023-02-07T19:00:00.000</t:Start>
          <t:End>2023-02-07T20:00:00.000</t:End>
        </t:CalendarItem>
      </m:Items>
    </m:CreateItem>
  </soap:Body>
</soap:Envelope>

前述の EWS 要求の例で作成された 3 つの予定を東部標準時で構成されているクライアントで表示すると、それぞれの予定が 1:00 PM、2:00 PM、および 3:00 PM に始まるように示されます。

特定のタイム ゾーンで予定を作成する方法を確認したら、 既存の予定のタイム ゾーン を別の予定に更新できます。

関連項目