Criar compromissos em um fuso horário específico usando o EWS no ExchangeCreate appointments in a specific time zone by using EWS in Exchange

Saiba como criar compromissos em fusos horários específicos usando a API gerenciada do EWS ou o EWS no Exchange.Learn how to create appointments in specific time zones by using the EWS Managed API or EWS in Exchange.

Quando um compromisso ou reunião é criado em um calendário do Exchange, o fuso horário usado para especificar as horas de início e término é salvo como o fuso horário de criação do compromisso.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. Esse fuso horário também é usado para interpretar valores de data e hora que não têm um fuso horário explícito especificado, portanto, é importante entender suas opções para especificar o fuso horário.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.

Criar compromissos em fusos horários diferentes usando a API gerenciada do EWSCreating appointments in different time zones by using the EWS Managed API

Ao criar compromissos ou reuniões usando a API gerenciada do EWS, você tem três opções para especificar o fuso horário:When creating appointments or meetings using the EWS Managed API, you have three options for specifying the time zone:

  • Para usar o fuso horário do computador em que a API gerenciada do EWS está sendo executada, não especifique um fuso horário ao criar o objeto 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.

  • Para usar um fuso horário específico para todas as propriedades de data/hora, incluindo propriedades ao criar um novo compromisso ou reunião, especifique um fuso horário no construtor para o objeto 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.

  • Para usar um fuso horário diferente daquele especificado na propriedade ExchangeService. TimeZone , use as propriedades compromisso. StartTimeZone e compromisso. endtimezone .To use a different time zone than the one specified in the ExchangeService.TimeZone property, use the Appointment.StartTimeZone and Appointment.EndTimeZone properties.

    Observação

    A propriedade endtimezone só está disponível quando a propriedade ExchangeService. RequestedServerVersion é definida como Exchange2010 ou posterior.The EndTimeZone property is only available when the ExchangeService.RequestedServerVersion property is set to Exchange2010 or later. Se ele não estiver disponível, a configuração do StartTimeZone se aplicará às horas de início e de término do compromisso.If it is not available, setting the StartTimeZone applies to both the start and end times of the appointment.

No exemplo a seguir, a API gerenciada do EWS é usada para criar três compromissos.In the following example, the EWS Managed API is used to create three appointments. Cada compromisso é definido para iniciar em 1:00 PM dois dias de agora, em um fuso horário não especificado, e terminar uma hora depois.Each appointment is set to start at 1:00 PM two days from now, in an unspecified time zone, and end one hour later. O primeiro compromisso é criado no fuso horário do computador cliente usando o comportamento padrão de API gerenciada do EWS.The first appointment is created in the client computer's time zone by using default EWS Managed API behavior. O segundo é criado no fuso horário central usando as propriedades de compromisso. StartTimeZone e compromisso. endtimezone .The second is created in the Central time zone by using the Appointment.StartTimeZone and Appointment.EndTimeZone properties. O terceiro é criado no fuso horário das montanhas usando a propriedade ExchangeService. TimeZone .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);
    }
}

Quando este exemplo é executado em um computador cliente configurado no fuso horário do leste, e os três compromissos que ele cria são exibidos de um cliente configurado no fuso horário do leste, eles aparecem às 1:00 P.M., 2:00 PM e 3:00 PM, respectivamente.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.

Criar compromissos em fusos horários diferentes usando o EWSCreating appointments in different time zones by using EWS

Ao criar compromissos ou reuniões usando o EWS, você tem três opções para especificar o fuso horário:When creating appointments or meetings using EWS, you have three options for specifying the time zone:

O exemplo a seguir, a solicitação de operação CreateItem cria um compromisso usando UTC.The following example CreateItem operation request creates an appointment using UTC. Observe que o elemento TimeZoneContext , o elemento StartTimeZone e o elemento endtimezone estão ausentes.Notice that the TimeZoneContext element, the StartTimeZone element, and the EndTimeZone element are absent. Os valores do elemento inicial e final são expressos em 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>

O exemplo a seguir, a solicitação de operação CreateItem usa os elementos StartTimeZone e endtimezone para especificar o fuso horário central para o compromisso.The following example CreateItem operation request uses the StartTimeZone and EndTimeZone elements to specify the Central time zone for the appointment. Observe que o elemento TimeZoneContext está ausente.Notice that the TimeZoneContext element is absent. No entanto, se estiver presente, os valores dos elementos StartTimeZone e endtimezone substituirão seu valor.However, if it were present, the values of the StartTimeZone and EndTimeZone elements would override its value. Novamente, os valores do elemento inicial e final são expressos em 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>

O exemplo a seguir, solicitação de operação CreateItem define o elemento TimeZoneContext para o fuso horário das montanhas.The following example CreateItem operation request sets the TimeZoneContext element to the Mountain time zone. Observe que os elementos StartTimeZone e endtimezone estão ausentes.Notice that the StartTimeZone and EndTimeZone elements are absent. Novamente, os valores do elemento inicial e final são expressos em 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>

Quando os três compromissos criados pelas solicitações anteriores de exemplos do EWS são exibidos de um cliente configurado no fuso horário da costa leste, eles aparecem em 1:00 P.M., 2:00 PM e 3:00 PM, respectivamente.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.

Agora que você sabe como criar compromissos em fusos horários específicos, convém atualizar os fusos horários em compromissos existentes para um diferente.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.

Confira tambémSee also