Dela via


Getting suggested meeting times by using the EWS Managed API 2.0

You can use the Microsoft Exchange Web Services (EWS) Managed API to get suggested meetings times for users in your organization.

Last modified: October 13, 2012

Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010

Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.

To get suggested meeting time information

  1. Create a list of attendees from which to get availability information.

    List<AttendeeInfo> attendees = new List<AttendeeInfo>();
    
    attendees.Add(new AttendeeInfo()
    {
         SmtpAddress = "user1@contoso.com",
         AttendeeType = MeetingAttendeeType.Organizer
    });
    
    attendees.Add(new AttendeeInfo()
    {
         SmtpAddress = "user2@contoso.com",
         AttendeeType = MeetingAttendeeType.Required
    });
    
  2. Instantiate the AvailabilityOptions object by using values that specify the suggested meeting time data to return.

    // Specify suggested meeting time options.
    AvailabilityOptions myOptions = new AvailabilityOptions();
    myOptions.MeetingDuration = 60;
    myOptions.MaximumNonWorkHoursSuggestionsPerDay = 0;
    myOptions.GoodSuggestionThreshold = 49;
    myOptions.MinimumSuggestionQuality = SuggestionQuality.Good;
    myOptions.DetailedSuggestionsWindow = new TimeWindow(DateTime.Now.AddDays(4), DateTime.Now.AddDays(5));
    
  3. Return a set of suggested meeting times and identify the Exchange service. The following example shows how to retrieve suggested meeting time information by using an ExchangeService object named service.

    GetUserAvailabilityResults results = service.GetUserAvailability(attendees,
                                                                 new TimeWindow(DateTime.Now, DateTime.Now.AddDays(2)),
                                                                     AvailabilityData.Suggestions,
                                                                     myOptions);
    
  4. Display the suggested meeting times.

    // Display available meeting times.
    Console.WriteLine("Availability for {0} and {1}", attendees[0].SmtpAddress, attendees[1].SmtpAddress);
    Console.WriteLine();
    
    foreach (Suggestion suggestion in results.Suggestions)
    {
         Console.WriteLine(suggestion.Date);
         Console.WriteLine();
         foreach (TimeSuggestion timeSuggestion in suggestion.TimeSuggestions)
         {
              Console.WriteLine("Suggested meeting time:" + timeSuggestion.MeetingTime);
              Console.WriteLine();
         }
    }
    

Example

The following example shows how to get suggested meeting times for attendees of a prospective meeting.

private static void GetSuggestedMeetingTimes(ExchangeService service)
{
     // Create a list of attendees.
     List<AttendeeInfo> attendees = new List<AttendeeInfo>();

     attendees.Add(new AttendeeInfo()
     {
          SmtpAddress = "user1@contsos.com",
          AttendeeType = MeetingAttendeeType.Organizer
     });

     attendees.Add(new AttendeeInfo()
     {
          SmtpAddress = "user2@contoso.com",
          AttendeeType = MeetingAttendeeType.Required
     });

     // Specify suggested meeting time options.
     AvailabilityOptions myOptions = new AvailabilityOptions();
     myOptions.MeetingDuration = 60;
     myOptions.MaximumNonWorkHoursSuggestionsPerDay = 0;
     myOptions.GoodSuggestionThreshold = 49;
     myOptions.MinimumSuggestionQuality = SuggestionQuality.Good;
     myOptions.DetailedSuggestionsWindow = new TimeWindow(DateTime.Now.AddDays(4), DateTime.Now.AddDays(5));

     // Return a set of suggested meeting times.
     GetUserAvailabilityResults results = service.GetUserAvailability(attendees,
                                                                  new TimeWindow(DateTime.Now, DateTime.Now.AddDays(2)),
                                                                      AvailabilityData.Suggestions,
                                                                      myOptions);
     // Display available meeting times.
     Console.WriteLine("Availability for {0} and {1}", attendees[0].SmtpAddress, attendees[1].SmtpAddress);
     Console.WriteLine();

     foreach (Suggestion suggestion in results.Suggestions)
     {
          Console.WriteLine(suggestion.Date);
          Console.WriteLine();
          foreach (TimeSuggestion timeSuggestion in suggestion.TimeSuggestions)
          {
               Console.WriteLine("Suggested meeting time:" + timeSuggestion.MeetingTime);
               Console.WriteLine();
          }
     }
}

The following example shows the XML that is sent by using the GetUserAvailability method.

<?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_SP1" />
      <t:TimeZoneContext>
        <t:TimeZoneDefinition Name="(UTC-08:00) Pacific Time (US &amp; Canada)" Id="Pacific Standard Time">
          <t:Periods>
            <t:Period Bias="P0DT8H0M0.0S" Name="Standard" Id="Std" />
            <t:Period Bias="P0DT7H0M0.0S" Name="Daylight" Id="Dlt/1" />
            <t:Period Bias="P0DT7H0M0.0S" Name="Daylight" Id="Dlt/2007" />
          </t:Periods>
          <t:TransitionsGroups>
            <t:TransitionsGroup Id="0">
              <t:RecurringDayTransition>
                <t:To Kind="Period">Dlt/1</t:To>
                <t:TimeOffset>P0DT2H0M0.0S</t:TimeOffset>
                <t:Month>4</t:Month>
                <t:DayOfWeek>Sunday</t:DayOfWeek>
                <t:Occurrence>1</t:Occurrence>
              </t:RecurringDayTransition>
              <t:RecurringDayTransition>
                <t:To Kind="Period">Std</t:To>
                <t:TimeOffset>P0DT2H0M0.0S</t:TimeOffset>
                <t:Month>10</t:Month>
                <t:DayOfWeek>Sunday</t:DayOfWeek>
                <t:Occurrence>-1</t:Occurrence>
              </t:RecurringDayTransition>
            </t:TransitionsGroup>
            <t:TransitionsGroup Id="1">
              <t:RecurringDayTransition>
                <t:To Kind="Period">Dlt/2007</t:To>
                <t:TimeOffset>P0DT2H0M0.0S</t:TimeOffset>
                <t:Month>3</t:Month>
                <t:DayOfWeek>Sunday</t:DayOfWeek>
                <t:Occurrence>2</t:Occurrence>
              </t:RecurringDayTransition>
              <t:RecurringDayTransition>
                <t:To Kind="Period">Std</t:To>
                <t:TimeOffset>P0DT2H0M0.0S</t:TimeOffset>
                <t:Month>11</t:Month>
                <t:DayOfWeek>Sunday</t:DayOfWeek>
                <t:Occurrence>1</t:Occurrence>
              </t:RecurringDayTransition>
            </t:TransitionsGroup>
          </t:TransitionsGroups>
          <t:Transitions>
            <t:Transition>
              <t:To Kind="Group">0</t:To>
            </t:Transition>
            <t:AbsoluteDateTransition>
              <t:To Kind="Group">1</t:To>
              <t:DateTime>2007-01-01T08:00:00.000Z</t:DateTime>
            </t:AbsoluteDateTransition>
          </t:Transitions>
        </t:TimeZoneDefinition>
      </t:TimeZoneContext>
    </soap:Header>
    <soap:Body>
      <m:GetUserAvailabilityRequest>
        <m:MailboxDataArray>
          <t:MailboxData>
            <t:Email>
              <t:Address>user1@contoso.com</t:Address>
            </t:Email>
            <t:AttendeeType>Organizer</t:AttendeeType>
            <t:ExcludeConflicts>false</t:ExcludeConflicts>
          </t:MailboxData>
          <t:MailboxData>
            <t:Email>
              <t:Address>user2@contoso.com</t:Address>
            </t:Email>
            <t:AttendeeType>Required</t:AttendeeType>
            <t:ExcludeConflicts>false</t:ExcludeConflicts>
          </t:MailboxData>
        </m:MailboxDataArray>
        <t:SuggestionsViewOptions>
          <t:GoodThreshold>49</t:GoodThreshold>
          <t:MaximumResultsByDay>10</t:MaximumResultsByDay>
          <t:MaximumNonWorkHourResultsByDay>0</t:MaximumNonWorkHourResultsByDay>
          <t:MeetingDurationInMinutes>60</t:MeetingDurationInMinutes>
          <t:MinimumSuggestionQuality>Good</t:MinimumSuggestionQuality>
          <t:DetailedSuggestionsWindow>
            <t:StartTime>2011-10-25T00:00:00</t:StartTime>
            <t:EndTime>2011-10-26T00:00:00</t:EndTime>
          </t:DetailedSuggestionsWindow>
        </t:SuggestionsViewOptions>
      </m:GetUserAvailabilityRequest>
    </soap:Body>
  </soap:Envelope>

The following example shows the XML that is returned by using the GetUserAvailability method.

<?xml version="1.0" encoding="utf-8"?>
  <s:Envelope xmlns:s="https://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
      <h:ServerVersionInfo MajorVersion="14" MinorVersion="1" MajorBuildNumber="225" MinorBuildNumber="71" Version="Exchange2010_SP2" xmlns:h="https://schemas.microsoft.com/exchange/services/2006/types" xmlns="https://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
    </s:Header>
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <GetUserAvailabilityResponse xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
        <SuggestionsResponse>
          <ResponseMessage ResponseClass="Success">
            <ResponseCode>NoError</ResponseCode>
          </ResponseMessage>
          <SuggestionDayResultArray>
            <SuggestionDayResult xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
              <Date>2011-10-25T00:00:00</Date>
              <DayQuality>Excellent</DayQuality>
              <SuggestionArray>
                <Suggestion>
                  <MeetingTime>2011-10-25T08:00:00</MeetingTime>
                  <IsWorkTime>true</IsWorkTime>
                  <SuggestionQuality>Excellent</SuggestionQuality>
                  <AttendeeConflictDataArray>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                  </AttendeeConflictDataArray>
                </Suggestion>
                <Suggestion>
                  <MeetingTime>2011-10-25T08:30:00</MeetingTime>
                  <IsWorkTime>true</IsWorkTime>
                  <SuggestionQuality>Excellent</SuggestionQuality>
                  <AttendeeConflictDataArray>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                  </AttendeeConflictDataArray>
                </Suggestion>
                <Suggestion>
                  <MeetingTime>2011-10-25T09:00:00</MeetingTime>
                  <IsWorkTime>true</IsWorkTime>
                  <SuggestionQuality>Excellent</SuggestionQuality>
                  <AttendeeConflictDataArray>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                  </AttendeeConflictDataArray>
                </Suggestion>
                <Suggestion>
                  <MeetingTime>2011-10-25T12:30:00</MeetingTime>
                  <IsWorkTime>true</IsWorkTime>
                  <SuggestionQuality>Excellent</SuggestionQuality>
                  <AttendeeConflictDataArray>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                  </AttendeeConflictDataArray>
                </Suggestion>
                <Suggestion>
                  <MeetingTime>2011-10-25T13:00:00</MeetingTime>
                  <IsWorkTime>true</IsWorkTime>
                  <SuggestionQuality>Excellent</SuggestionQuality>
                  <AttendeeConflictDataArray>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                  </AttendeeConflictDataArray>
                </Suggestion>
                <Suggestion>
                  <MeetingTime>2011-10-25T16:00:00</MeetingTime>
                  <IsWorkTime>true</IsWorkTime>
                  <SuggestionQuality>Excellent</SuggestionQuality>
                  <AttendeeConflictDataArray>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                  </AttendeeConflictDataArray>
                </Suggestion>
                <Suggestion>
                  <MeetingTime>2011-10-25T13:30:00</MeetingTime>
                  <IsWorkTime>true</IsWorkTime>
                  <SuggestionQuality>Good</SuggestionQuality>
                  <AttendeeConflictDataArray>
                    <IndividualAttendeeConflictData>
                      <BusyType>Tentative</BusyType>
                    </IndividualAttendeeConflictData>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                  </AttendeeConflictDataArray>
                </Suggestion>
                <Suggestion>
                  <MeetingTime>2011-10-25T09:30:00</MeetingTime>
                  <IsWorkTime>true</IsWorkTime>
                  <SuggestionQuality>Good</SuggestionQuality>
                  <AttendeeConflictDataArray>
                    <IndividualAttendeeConflictData>
                      <BusyType>Tentative</BusyType>
                    </IndividualAttendeeConflictData>
                    <IndividualAttendeeConflictData>
                      <BusyType>Tentative</BusyType>
                    </IndividualAttendeeConflictData>
                  </AttendeeConflictDataArray>
                </Suggestion>
                <Suggestion>
                  <MeetingTime>2011-10-25T14:00:00</MeetingTime>
                  <IsWorkTime>true</IsWorkTime>
                  <SuggestionQuality>Good</SuggestionQuality>
                  <AttendeeConflictDataArray>
                    <IndividualAttendeeConflictData>
                      <BusyType>Tentative</BusyType>
                    </IndividualAttendeeConflictData>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                  </AttendeeConflictDataArray>
                </Suggestion>
                <Suggestion>
                  <MeetingTime>2011-10-25T12:00:00</MeetingTime>
                  <IsWorkTime>true</IsWorkTime>
                  <SuggestionQuality>Good</SuggestionQuality>
                  <AttendeeConflictDataArray>
                    <IndividualAttendeeConflictData>
                      <BusyType>Busy</BusyType>
                    </IndividualAttendeeConflictData>
                    <IndividualAttendeeConflictData>
                      <BusyType>Free</BusyType>
                    </IndividualAttendeeConflictData>
                  </AttendeeConflictDataArray>
                </Suggestion>
              </SuggestionArray>
            </SuggestionDayResult>
          </SuggestionDayResultArray>
        </SuggestionsResponse>
      </GetUserAvailabilityResponse>
    </s:Body>
  </s:Envelope>

Compiling the code

For information about compiling this code, see Getting started with the EWS Managed API 2.0.

Robust programming

  • Write appropriate error handling code for common search errors.

  • Review the client request XML that is sent to the Exchange server.

  • Review the server response XML that is sent from the Exchange server.

  • Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API 2.0. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server. If the client cannot connect to the service, retry setting the binding by using the AutodiscoverUrl(String) method.

  • Set the target Exchange Web Services schema version by setting the requestedServerVersion parameter of the ExchangeService constructor. For more information, see Versioning EWS requests by using the EWS Managed API 2.0.

Security

  • Use HTTP with SSL for all communication between client and server.

  • Always validate the server certificate that is used for establishing the SSL connections. For more information, see Validating X509 certificates by using the EWS Managed API 2.0.

  • Do not include user names and passwords in trace files.

  • Verify that Autodiscover lookups that use HTTP GET to find an endpoint always prompt for user confirmation; otherwise, they should be blocked.