2 out of 4 rated this helpful - Rate this topic

Using Autodiscover with Exchange Online

Exchange Server 2010

Summary:  Find information about using Autodiscover with Exchange Online. This article includes a code example that shows you how to use the Autodiscover service.

Applies to:  Microsoft Exchange Online

Published:   March 2011

Provided by:  Steve Petersen, Microsoft Corporation

To use Exchange Web Services (EWS) to develop for Exchange Online, you need to identify the EWS endpoints that the client application has to use to retrieve configuration settings. The Autodiscover service enables you to discover these endpoints. In this article, I describe how to use the Autodiscover service to request information about the EWS endpoints.

Before you begin

You must be connected to an Exchange Online server to use the Autodiscover service. For information about how to connect to Exchange Online and bind your application to the Exchange Online service, see Getting started with Exchange Online.

Prerequisites

The following are prerequisites to using the Autodiscover service with Exchange Online:

  • You must have an Exchange Online domain name and user account. Before you can develop against Exchange Online, an administrator must add you to the system by creating a user account. When the administrator creates the user account, a Windows Live ID for the new user is also created that includes the user's email address and password.

  • The Exchange Online server must be running Microsoft Exchange Server 2010 or Microsoft Exchange Server 2010 Service Pack 1 (SP1). If the Exchange Server version is unknown, ask your system administrator.

  • You must be an authorized user on the Exchange Online server. Your credentials (user name and password) must authenticate against the Autodiscover service.

  • If you are using the EWS Managed API to access the Autodiscover service, you must have the API installed. To download the EWS Managed API, see the Microsoft Download Center. For more information about the EWS Managed API, see the Microsoft Exchange Web Services Managed API 1.1 SDK.

Autodiscover service

The Autodiscover service provides the configuration information that you need to create a connection to an Exchange Online server. Using Autodiscover to identify the EWS endpoint is more reliable than hardcoding the URL.

In this article, I use the SOAP Autodiscover service, which uses SOAP as the messaging framework for communications that are sent between the client and the server. For more information about the SOAP Autodiscover service, see Autodiscover Service (SOAP). If you do not want to use SOAP, you can use the "plain old XML" (POX) Autodiscover service, which consists of XML payloads only. For more information, see Autodiscover Service (POX). For a more detailed explanation of the two different types of mechanisms, see Working with Autodiscover.

To use the Autodiscover service, set the service binding by using the AutodiscoverUrl method, as shown in the example later in this article. 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 method.

Gg591268.note(en-us,EXCHG.140).gifNote:

In an Exchange Online application, the AutodiscoverUrl method requires a validation callback. If you try to use the overloaded AutodiscoverUrl method without an AutodiscoverRedirectionUrlValidationCallback method, an error will occur. To enable Autodiscover to follow the redirection, use the AutodiscoverUrl(string, AutodiscoverRedirectionUrlValidationCallback) method overload.

To use the Exchange Web Services (EWS) Managed API, you need an instance of the ExchangeService class, as shown in the example later in this article. The binding requires the user's Windows Live ID and credentials to discover the EWS endpoints.

After the service binding is completed, you can use EWS service calls to complete other EWS functions, such as sending email messages, creating rules, creating contacts, and managing streaming notifications. For more information about using EWS Managed API to perform these tasks, see the Microsoft Exchange Web Services Managed API 1.1 SDK.

Autodiscover process

Send an Autodiscover request by using the email address of the user for whom you are requesting Exchange configuration settings; for example, user@Contosodomain.com. The email address is used to determine the Autodiscover service endpoints. When you send the Autodiscover request, the following occurs:

  1. Exchange Online contacts the Domain Name System (DNS), and DNS resolves the domain name.

  2. The DNS server redirects users to the actual Autodiscover service.

  3. The tenant activity redirects to a specific tenant.

  4. The results are returned.

  5. The settings are specified. The returned response contains the configuration setting, including the URL for the EWS service endpoint.

  6. The EwsUrl element contained within the settings holds the EWS URL. The managed AutodiscoverURL method, after a satisfactory response, sets the service URL.

In the example that I include in this article, the following takes place:

  1. The domain name Contosodomain.com is resolved to autodiscover.domain.com.

  2. The request is redirected to autodiscover-s.outlook.com/Autodiscover/Autodiscover.xml/.

  3. Autodiscover-s.outlook.com/Autodiscover/Autodiscover.xml is redirected to the specific tenant activity — in this case, pod51008.outlook.com/Autodiscover/Autodiscover.xml.

  4. Pod51008.outlook.com/Autodiscover/Autodiscover.xml returns the results for the specified user.

  5. The returned response contains the URL for the EWS service endpoint.

  6. The managed AutodiscoverURL method sets the service URL.

Example

The following example shows you how to use the Autodiscover service, by using the AutodiscoverURL method, to identify an endpoint. This example uses the AutodiscoverUrl method to locate the EWS endpoint for the server, sets the service URL, creates a binding, and displays the service URL in the console. This example assumes that there is a valid User1 on the Contosodomain account, which is located on the Exchange Online server that the account is using.

The RedirectionUrlValidationCallback method verifies that the redirection address is valid. The RedirectionUrlValidationCallback method returns true if Autodiscover is permitted to use the returned URL. In this example, the console displays the URL of the Autodiscover service.

using System;
using Microsoft.Exchange.WebServices.Data;
using Microsoft.Exchange.WebServices.Autodiscover;

namespace EWS_Exchange_Online_CS
{
    class Program
    {
        static void Main()
        {
            // Create a service binding and reuse it for each EWS call.
            ExchangeService service = GetBinding();

            // Pause the console.
            ConsoleKeyInfo cki;
            Console.WriteLine("\n\nPress E to exit");
            while (true)
            {
                cki = Console.ReadKey(true);
                {
                    if (cki.Key == ConsoleKey.E) break;
                }
            }
        }

        static ExchangeService GetBinding()
        {
            // Create the binding.
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);

            // Define credentials.
            service.Credentials = new WebCredentials("User1@Contosodomain.onmicrosoft.com", "pass@word1");

            // Use the AutodiscoverUrl method to locate the service endpoint.
            try
            {
                service.AutodiscoverUrl("User1@Contosodomain.onmicrosoft.com", RedirectionUrlValidationCallback);
            }
            catch (AutodiscoverRemoteException ex)
            {
                Console.WriteLine("Exception thrown: " + ex.Error.Message);
            }

            // Display the service URL.
            Console.WriteLine("AutodiscoverURL: " + service.Url);
            return service;
        }

        // Create the callback to validate the redirection URL.
        static bool RedirectionUrlValidationCallback(String redirectionUrl)
        {
            // Perform validation.
            return (redirectionUrl == "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml");
        }
    }
}

XML request

The following example shows a valid Autodiscover request that is generated from the code used in the previous example.

<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006">
  <Request>
     <EMailAddress>User1@Contosodomain.onmicrosoft.com</EMailAddress>
    <AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/
      outlook/responseschema/2006a</AcceptableResponseSchema>
  </Request>
</Autodiscover>

A valid Autodiscover response or an HTTP 302 redirect response is returned from a valid Autodiscover request.

XML response

The following example shows a valid Autodiscover response. The EwsUrl element contains the URL for the endpoint for EWS.

<?xml version="1.0" encoding="utf-8"?>
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
  <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
    <User>
      <DisplayName>User1 New</DisplayName>
      <LegacyDN>/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/
        cn=Recipients/cn=Contosodomain.onmicrosoft.com-55760-User1</LegacyDN>
      <DeploymentId>e7908b08-1b41-4665-ba7f-33677ce0cf32</DeploymentId>
    </User>
    <Account>
      <AccountType>email</AccountType>
      <Action>settings</Action>
      <Protocol>
        <Type>EXCH</Type>
        <Server>CH1PRD0302.mailbox.outlook.com</Server>
        <ServerDN>/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/
          cn=Configuration/cn=Servers/cn=CH1PRD0302.mailbox.outlook.com</ServerDN>
        <ServerVersion>738180E1</ServerVersion>
        <MdbDN>/o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Configuration/
          cn=Servers/cn=CH1PRD0302.mailbox.outlook.com/cn=Microsoft Private MDB</MdbDN>
        <AD>CH1PRD0301DC011.namprd03.prod.outlook.com</AD>
        <ASUrl>https://ch1prd0302.outlook.com/EWS/Exchange.asmx</ASUrl>
        <EwsUrl>https://ch1prd0302.outlook.com/EWS/Exchange.asmx</EwsUrl>
        <SharingUrl>https://ch1prd0302.outlook.com/EWS/Exchange.asmx</SharingUrl>
        <EcpUrl>https://ch1prd0302ca006.namprd03.prod.outlook.com/ecp</EcpUrl>
        <EcpUrl-um>?p=customize/voicemail.aspx&amp;exsvurl=1&amp;
          realm=Contosodomain.onmicrosoft.com</EcpUrl-um>
        <EcpUrl-aggr>?p=personalsettings/EmailSubscriptions.slab&amp;
          exsvurl=1&amp;realm=Contosodomain.onmicrosoft.com</EcpUrl-aggr>
        <EcpUrl-mt>PersonalSettings/DeliveryReport.aspx?exsvurl=1&amp;IsOWA=&lt;IsOWA&gt;&amp;MsgID=&lt;
          MsgID&gt;&amp;Mbx=&lt;Mbx&gt;&amp;realm=Contosodomain.onmicrosoft.com</EcpUrl-mt>
        <EcpUrl-sms>?p=sms/textmessaging.slab&amp;exsvurl=1&amp;realm=Contosodomain.onmicrosoft.com</EcpUrl-sms>
        <OOFUrl>https://ch1prd0302.outlook.com/EWS/Exchange.asmx</OOFUrl>
        <UMUrl>https://ch1prd0302.outlook.com/EWS/UM2007Legacy.asmx</UMUrl>
        <OABUrl>https://ch1prd0302.outlook.com/OAB/d1bcbba5-a450-4b17-a236-be759e1a2dbe/</OABUrl>
        <ServerExclusiveConnect>off</ServerExclusiveConnect>
      </Protocol>
      <Protocol>
        <Type>EXPR</Type>
        <Server>pod51008.outlook.com</Server>
        <SSL>On</SSL>
        <AuthPackage>Basic</AuthPackage>
        <ASUrl>https://ch1prd0302.outlook.com/EWS/Exchange.asmx</ASUrl>
        <EwsUrl>https://ch1prd0302.outlook.com/EWS/Exchange.asmx</EwsUrl>
        <SharingUrl>https://ch1prd0302.outlook.com/EWS/Exchange.asmx</SharingUrl>
        <EcpUrl>https://ch1prd0302.outlook.com/ecp/</EcpUrl>
        <EcpUrl-um>?p=customize/voicemail.aspx&amp;exsvurl=1&amp;
          realm=Contosodomain.onmicrosoft.com</EcpUrl-um>
        <EcpUrl-aggr>?p=personalsettings/EmailSubscriptions.slab&amp;exsvurl=1&amp;
          realm=Contosodomain.onmicrosoft.com</EcpUrl-aggr>
        <EcpUrl-mt>PersonalSettings/DeliveryReport.aspx?
          exsvurl=1&amp;IsOWA=&lt;IsOWA&gt;&amp;MsgID=&lt;MsgID&gt;&amp;Mbx=&lt;Mbx&gt;
          &amp;realm=Contosodomain.onmicrosoft.com</EcpUrl-mt>
        <EcpUrl-sms>?p=sms/textmessaging.slab&amp;exsvurl=1&amp;
          realm=Contosodomain.onmicrosoft.com</EcpUrl-sms>
        <OOFUrl>https://ch1prd0302.outlook.com/EWS/Exchange.asmx</OOFUrl>
        <UMUrl>https://ch1prd0302.outlook.com/EWS/UM2007Legacy.asmx</UMUrl>
        <OABUrl>https://ch1prd0302.outlook.com/OAB/d1bcbba5-a450-4b17-a236-be759e1a2dbe/</OABUrl>
        <ServerExclusiveConnect>on</ServerExclusiveConnect>
        <CertPrincipalName>msstd:outlook.com</CertPrincipalName>
      </Protocol>
      <Protocol>
        <Type>WEB</Type>
        <Internal>
          <OWAUrl AuthenticationMethod="LiveIdFba">
            https://ch1prd0302ca001.namprd03.prod.outlook.com/owa/</OWAUrl>
          <OWAUrl AuthenticationMethod="LiveIdFba">
            https://ch1prd0302ca002.namprd03.prod.outlook.com/owa/</OWAUrl>
          <OWAUrl AuthenticationMethod="LiveIdFba">
            https://ch1prd0302ca003.namprd03.prod.outlook.com/owa/</OWAUrl>
          <OWAUrl AuthenticationMethod="LiveIdFba">
            https://ch1prd0302ca004.namprd03.prod.outlook.com/owa/</OWAUrl>
          <OWAUrl AuthenticationMethod="LiveIdFba">
            https://ch1prd0302ca005.namprd03.prod.outlook.com/owa/</OWAUrl>
          <OWAUrl AuthenticationMethod="LiveIdFba">
             https://ch1prd0302ca006.namprd03.prod.outlook.com/owa/</OWAUrl>
          <Protocol>
            <Type>EXCH</Type>
            <ASUrl>https://ch1prd0302.outlook.com/EWS/Exchange.asmx</ASUrl>
          </Protocol>
        </Internal>
        <External>
          <OWAUrl AuthenticationMethod="Fba">https://ch1prd0302.outlook.com/owa/</OWAUrl>
          <Protocol>
            <Type>EXPR</Type>
            <ASUrl>https://ch1prd0302.outlook.com/EWS/Exchange.asmx</ASUrl>
          </Protocol>
        </External>
      </Protocol>
    </Account>
  </Response>
</Autodiscover>

Related topics

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.