Getting user free/busy information by using the EWS Managed API 2.0

You can use the Microsoft Exchange Web Services (EWS) Managed API to get free/busy information 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 user free/busy information

  1. Create a list of attendees from which to get free/busy information.

    // Create a list of attendees.
    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 free/busy data to return.

    // Specify availability options.
    AvailabilityOptions myOptions = new AvailabilityOptions();
    myOptions.MeetingDuration = 30;
    myOptions.RequestedFreeBusyView = FreeBusyViewType.FreeBusy;
    
  3. Return a set of free/busy times and identify the Exchange service. The following example shows how to retrieve Out Of Office (OOF) information by using an ExchangeService object named service.

    // Return a set of free/busy times.
    GetUserAvailabilityResults freeBusyResults = service.GetUserAvailability(attendees, 
                                                                         new TimeWindow(DateTime.Now, DateTime.Now.AddDays(1)), 
                                                                             AvailabilityData.FreeBusy, 
                                                                             myOptions);
    
  4. Display the free/busy information for the attendees.

    // Display available meeting times.
    Console.WriteLine("Availability for {0} and {1}", attendees[0].SmtpAddress, attendees[1].SmtpAddress);
    Console.WriteLine();
    
    foreach (AttendeeAvailability availability in freeBusyResults.AttendeesAvailability)
    {
         Console.WriteLine(availability.Result);
         Console.WriteLine();
         foreach (CalendarEvent calendarItem in availability.CalendarEvents)
         {
              Console.WriteLine("Free/busy status: " + calendarItem.FreeBusyStatus);
              Console.WriteLine("Start time: " + calendarItem.StartTime);
              Console.WriteLine("End time: " + calendarItem.EndTime);
              Console.WriteLine();
         }
    }
    

Example

The following example shows how to get free/busy information for attendees of a meeting.

private static void GetUserFreeBusy(ExchangeService service)
{
     // Create a list of attendees.
     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
     });

     // Specify availability options.
     AvailabilityOptions myOptions = new AvailabilityOptions();
     myOptions.MeetingDuration = 30;
     myOptions.RequestedFreeBusyView = FreeBusyViewType.FreeBusy;

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

     foreach (AttendeeAvailability availability in freeBusyResults.AttendeesAvailability)
     {
         Console.WriteLine(availability.Result);
         Console.WriteLine();
         foreach (CalendarEvent calendarItem in availability.CalendarEvents)
         {
             Console.WriteLine("Free/busy status: " + calendarItem.FreeBusyStatus);
             Console.WriteLine("Start time: " + calendarItem.StartTime);
             Console.WriteLine("End time: " + calendarItem.EndTime);
             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>greg@fabrikam.com</t:Address>
            </t:Email>
            <t:AttendeeType>Organizer</t:AttendeeType>
            <t:ExcludeConflicts>false</t:ExcludeConflicts>
          </t:MailboxData>
          <t:MailboxData>
            <t:Email>
              <t:Address>karim@fabrikam.com</t:Address>
            </t:Email>
            <t:AttendeeType>Required</t:AttendeeType>
            <t:ExcludeConflicts>false</t:ExcludeConflicts>
          </t:MailboxData>
        </m:MailboxDataArray>
        <t:FreeBusyViewOptions>
          <t:TimeWindow>
            <t:StartTime>2011-10-22T00:00:00</t:StartTime>
            <t:EndTime>2011-10-23T00:00:00</t:EndTime>
          </t:TimeWindow>
          <t:MergedFreeBusyIntervalInMinutes>30</t:MergedFreeBusyIntervalInMinutes>
          <t:RequestedView>FreeBusy</t:RequestedView>
        </t:FreeBusyViewOptions>
      </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">
        <FreeBusyResponseArray>
          <FreeBusyResponse>
            <ResponseMessage ResponseClass="Success">
              <ResponseCode>NoError</ResponseCode>
            </ResponseMessage>
            <FreeBusyView>
              <FreeBusyViewType xmlns="https://schemas.microsoft.com/exchange/services/2006/types">FreeBusy</FreeBusyViewType>
              <WorkingHours xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
                <TimeZone>
                  <Bias>480</Bias>
                  <StandardTime>
                    <Bias>0</Bias>
                    <Time>02:00:00</Time>
                    <DayOrder>1</DayOrder>
                    <Month>11</Month>
                    <DayOfWeek>Sunday</DayOfWeek>
                  </StandardTime>
                  <DaylightTime>
                    <Bias>-60</Bias>
                    <Time>02:00:00</Time>
                    <DayOrder>2</DayOrder>
                    <Month>3</Month>
                    <DayOfWeek>Sunday</DayOfWeek>
                  </DaylightTime>
                </TimeZone>
                <WorkingPeriodArray>
                  <WorkingPeriod>
                    <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
                    <StartTimeInMinutes>480</StartTimeInMinutes>
                    <EndTimeInMinutes>1020</EndTimeInMinutes>
                  </WorkingPeriod>
                </WorkingPeriodArray>
              </WorkingHours>
            </FreeBusyView>
          </FreeBusyResponse>
          <FreeBusyResponse>
            <ResponseMessage ResponseClass="Success">
              <ResponseCode>NoError</ResponseCode>
            </ResponseMessage>
            <FreeBusyView>
              <FreeBusyViewType xmlns="https://schemas.microsoft.com/exchange/services/2006/types">FreeBusy</FreeBusyViewType>
              <CalendarEventArray xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
                <CalendarEvent>
                  <StartTime>2011-10-20T00:00:00</StartTime>
                  <EndTime>2011-10-23T00:00:00</EndTime>
                  <BusyType>OOF</BusyType>
                </CalendarEvent>
              </CalendarEventArray>
              <WorkingHours xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
                <TimeZone>
                  <Bias>480</Bias>
                  <StandardTime>
                    <Bias>0</Bias>
                    <Time>02:00:00</Time>
                    <DayOrder>1</DayOrder>
                    <Month>11</Month>
                    <DayOfWeek>Sunday</DayOfWeek>
                  </StandardTime>
                  <DaylightTime>
                    <Bias>-60</Bias>
                    <Time>02:00:00</Time>
                    <DayOrder>2</DayOrder>
                    <Month>3</Month>
                    <DayOfWeek>Sunday</DayOfWeek>
                  </DaylightTime>
                </TimeZone>
                <WorkingPeriodArray>
                  <WorkingPeriod>
                    <DayOfWeek>Monday Tuesday Wednesday Thursday Friday</DayOfWeek>
                    <StartTimeInMinutes>480</StartTimeInMinutes>
                    <EndTimeInMinutes>960</EndTimeInMinutes>
                  </WorkingPeriod>
                </WorkingPeriodArray>
              </WorkingHours>
            </FreeBusyView>
          </FreeBusyResponse>
        </FreeBusyResponseArray>
      </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.