Getting User Availability in Exchange 2010

Last modified: May 21, 2009

Applies to: Exchange Server 2007 | Exchange Server 2010

Use Exchange Web Services to access a user's free/busy information.

Example

The following example shows you how to get the merged free/busy stream for a client in the Coordinated Universal Time -08:00 time zone by using updated information for the time zone changes that are made for daylight saving time.

static void GetUserAvailability(ExchangeServiceBinding esb)
{
    // Identify the time to compare free/busy information.
    Duration duration = new Duration();
    duration.StartTime = DateTime.Now;
    duration.EndTime = DateTime.Now.AddHours(4);

    // Identify the options for comparing free/busy information.
    FreeBusyViewOptionsType fbViewOptions = new FreeBusyViewOptionsType();
    fbViewOptions.TimeWindow = duration;
    fbViewOptions.RequestedView = FreeBusyViewType.MergedOnly;
    fbViewOptions.RequestedViewSpecified = true;
    fbViewOptions.MergedFreeBusyIntervalInMinutes = 35;
    fbViewOptions.MergedFreeBusyIntervalInMinutesSpecified = true;

    MailboxData[] mailboxes = new MailboxData[1];
    mailboxes[0] = new MailboxData();

    // Identify the user mailbox to review for free/busy data.
    EmailAddress emailAddress = new EmailAddress();

    emailAddress.Address = "tplate@contoso.com";
    emailAddress.Name = String.Empty;

    mailboxes[0].Email = emailAddress;
    mailboxes[0].ExcludeConflicts = false;

    // Make the request.
    GetUserAvailabilityRequestType request = new GetUserAvailabilityRequestType();

    // Set the time zone of the request.
    request.TimeZone = new SerializableTimeZone();
    request.TimeZone.Bias = 480;
    request.TimeZone.StandardTime = new SerializableTimeZoneTime();
    request.TimeZone.StandardTime.Bias = 0;
    request.TimeZone.StandardTime.DayOfWeek = DayOfWeekType.Sunday.ToString();
    request.TimeZone.StandardTime.DayOrder = 1;
    request.TimeZone.StandardTime.Month = 11;
    request.TimeZone.StandardTime.Time = "02:00:00";
    request.TimeZone.DaylightTime = new SerializableTimeZoneTime();
    request.TimeZone.DaylightTime.Bias = -60;
    request.TimeZone.DaylightTime.DayOfWeek = DayOfWeekType.Sunday.ToString();
    request.TimeZone.DaylightTime.DayOrder = 2;
    request.TimeZone.DaylightTime.Month = 3;
    request.TimeZone.DaylightTime.Time = "02:00:00";

    // Add the mailboxes to the request.
    request.MailboxDataArray = mailboxes;

    // Add the view options to the request.
    request.FreeBusyViewOptions = fbViewOptions;

    try
    {
        // Send the request and get the response.
        GetUserAvailabilityResponseType response = esb.GetUserAvailability(request);

        // Access free/busy information.
        if (response.FreeBusyResponseArray.Length < 1)
        {
            throw new Exception("No free/busy response data available.");
        }
        else
        {
            foreach (FreeBusyResponseType fbrt in response.FreeBusyResponseArray)
            {
                if (fbrt.ResponseMessage.ResponseClass == ResponseClassType.Error)
                {
                    Console.WriteLine(string.Format("Error: {0}", fbrt.ResponseMessage.MessageText));
                }
                else
                {
                    // Show the free/busy stream.
                    FreeBusyView fbv = fbrt.FreeBusyView;
                    Console.WriteLine(string.Format("Merged free/busy data: {0}", fbv.MergedFreeBusy));
                }
            }
        }
    }
    catch (Exception e)
    {
        // Perform error processing.
        Console.WriteLine(e.Message);
        Console.ReadLine();
    }
}

The following XML example shows the XML request message that is sent from the client to the server.

<GetUserAvailabilityRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                            xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <TimeZone xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
    <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>
  <MailboxDataArray xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <MailboxData xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
      <Email>
        <Name />
        <Address>tplate@contoso.com</Address>
      </Email>
      <AttendeeType>Organizer</AttendeeType>
    </MailboxData>
  </MailboxDataArray>
  <FreeBusyViewOptions xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
    <TimeWindow>
      <StartTime>2006-08-18T16:22:44.1702229-07:00</StartTime>
      <EndTime>2006-08-18T20:22:44.1702229-07:00</EndTime>
    </TimeWindow>
    <MergedFreeBusyIntervalInMinutes>35</MergedFreeBusyIntervalInMinutes>
    <RequestedView>MergedOnly</RequestedView>
  </FreeBusyViewOptions>
</GetUserAvailabilityRequest>

The following XML example shows the XML response message that is sent from the server to the client.

<GetUserAvailabilityResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                             xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <FreeBusyResponseArray xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <FreeBusyResponse>
      <ResponseMessage ResponseClass="Success">
        <ResponseCode>NoError</ResponseCode>
      </ResponseMessage>
      <FreeBusyView>
        <FreeBusyViewType xmlns="https://schemas.microsoft.com/exchange/services/2006/types">MergedOnly</FreeBusyViewType>
        <MergedFreeBusy xmlns="https://schemas.microsoft.com/exchange/services/2006/types">0000000</MergedFreeBusy>
        <WorkingHours xmlns="https://schemas.microsoft.com/exchange/services/2006/types">
          <TimeZone>
            <Bias>480</Bias>
            <StandardTime>
              <Bias>0</Bias>
              <Time>02:00:00</Time>
              <DayOrder>5</DayOrder>
              <Month>10</Month>
              <DayOfWeek>Sunday</DayOfWeek>
            </StandardTime>
            <DaylightTime>
              <Bias>-60</Bias>
              <Time>02:00:00</Time>
              <DayOrder>1</DayOrder>
              <Month>4</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>
  </FreeBusyResponseArray>
</GetUserAvailabilityResponse>

The SOAP messages that are passed between the Exchange Web Services client and server are defined by the XML schema and WSDL files. The XML schema and WSDL files define the contract between the client and server. Proxy class generators create an object-model abstraction of those SOAP messages, which can simplify programming. This code example uses a proxy class library that was generated by MicrosoftVisual Studio 2005. Different proxy class generators create different object models for a given Web service. This proxy class code example is an illustration only. Refer to the proxy class generator documentation for support for proxy classes.