Getting a room by using the EWS Managed API 2.0

You can use the Microsoft Exchange Web Services (EWS) Managed API to get a collection of rooms from a room list, retrieve the collection that matches your search criteria, and expand the selected collection in order to locate a room that meets your specified criteria.

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 find a room in a collection of rooms from a room list

  1. Instantiate the EmailAddressCollection object by calling the GetRoomLists method of an ExchangeService object named service.

    // Return all the room lists in the organization.
    EmailAddressCollection myRoomLists = service.GetRoomLists();
    
  2. Retrieve the room list collection that matches your criteria.

    // Retrieve the room list that matches your criteria.
    EmailAddress myAddress = new EmailAddress("user1@contoso.com");
    foreach (EmailAddress address in myRoomLists)
    {
         if (address == myAddress)
         {
              Console.WriteLine("Found {0} in room list", myAddress);
         }
    }
    
  3. Expand the selected collection to get a list of rooms.

    System.Collections.ObjectModel.Collection<EmailAddress> myRoomAddresses = service.GetRooms(myAddress);
    
  4. Display the rooms.

    // Display the individual rooms.
    foreach (EmailAddress address in myRoomAddresses)
                {
                    Console.WriteLine("Email Address: {0}", address.Address);
                }
    

Example

The following example shows how to return a collection of rooms from a room list and expand the collection to display the members.

private static void GetRooms(ExchangeService service)
{
     // Return all the room lists in the organization
     EmailAddressCollection myRoomLists = service.GetRoomLists();

     // Retrieve the room list that matches your criteria
     EmailAddress myAddress = new EmailAddress("building31@contoso.com");
     foreach (EmailAddress address in myRoomLists)
     {
          if (address == myAddress)
          {
               Console.WriteLine("Found {0} in room list", myAddress);
          }
     }

     // Expand the selected collection to get a list of rooms.
     System.Collections.ObjectModel.Collection<EmailAddress> myRoomAddresses = service.GetRooms(myAddress);

     // Display the individual rooms.
     foreach (EmailAddress address in myRoomAddresses)
     {
          Console.WriteLine("Email Address: {0}", address.Address);
     }
}

The following example shows the XML that is sent by using the GetRoomLists 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_SP2" />
    </soap:Header>
    <soap:Body>
      <m:GetRoomLists />
    </soap:Body>
  </soap:Envelope>

The following example shows the XML that is returned by the GetRoomLists 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">
      <GetRoomListsResponse ResponseClass="Success" xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
        <ResponseCode>NoError</ResponseCode>
        <m:RoomLists xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages">
          <t:Address xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
            <t:Name>Contoso</t:Name>
            <t:EmailAddress>Building31@contoso.com</t:EmailAddress>
            <t:RoutingType>SMTP</t:RoutingType>
            <t:MailboxType>PublicDL</t:MailboxType>
          </t:Address>
          <t:Address xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
            <t:Name>FABRIKAM2</t:Name>
            <t:EmailAddress>Building32@contoso.com</t:EmailAddress>
            <t:RoutingType>SMTP</t:RoutingType>
            <t:MailboxType>PublicDL</t:MailboxType>
          </t:Address>
        </m:RoomLists>
      </GetRoomListsResponse>
    </s:Body>
  </s:Envelope>

The following example shows the XML that is sent by using the GetRooms 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" />
    </soap:Header>
    <soap:Body>
      <m:ExpandDL>
        <m:Mailbox>
          <t:EmailAddress>building31@contoso.com</t:EmailAddress>
        </m:Mailbox>
      </m:ExpandDL>
    </soap:Body>
  </soap:Envelope>

The following example shows the XML that is returned by the GetRooms 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">
      <m:ExpandDLResponse xmlns:m="https://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="https://schemas.microsoft.com/exchange/services/2006/types">
        <m:ResponseMessages>
          <m:ExpandDLResponseMessage ResponseClass="Success">
            <m:ResponseCode>NoError</m:ResponseCode>
            <m:DLExpansion TotalItemsInView="3" IncludesLastItemInRange="true">
              <t:Mailbox>
                <t:Name>Conf Room 31</t:Name>
                <t:EmailAddress>ConfRoom31@contoso.com</t:EmailAddress>
                <t:RoutingType>SMTP</t:RoutingType>
                <t:MailboxType>Mailbox</t:MailboxType>
              </t:Mailbox>
              <t:Mailbox>
                <t:Name>Conf Room 32</t:Name>
                <t:EmailAddress>ConfRoom32@contoso.com</t:EmailAddress>
                <t:RoutingType>SMTP</t:RoutingType>
                <t:MailboxType>Mailbox</t:MailboxType>
              </t:Mailbox>
              <t:Mailbox>
                <t:Name>Conf Room 33</t:Name>
                <t:EmailAddress>ConfRoom33@contoso.com</t:EmailAddress>
                <t:RoutingType>SMTP</t:RoutingType>
                <t:MailboxType>Mailbox</t:MailboxType>
              </t:Mailbox>
            </m:DLExpansion>
          </m:ExpandDLResponseMessage>
        </m:ResponseMessages>
      </m:ExpandDLResponse>
    </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.