locked
(EWS) How to call autodiscover URL to retrieve exchange service URL? RRS feed

  • Question

  • Hi Folks,

    I have some code that finds the autodiscover url by searching on the SCP.  What I would like to do is then call the auto discover service at that location to determine the URL of the ExchangeService for making EWS calls.

    The examples I can find in the MS documentation use an email address (ExchangeService.AutoDiscoverURL  and GetUserSettingsResponse).

    What I would like to do is call the service located at the address given by the SCP and then set the ExchangeService.Url property.

    I am aware that I need to use the ASUrl property of the AutoDiscover Response (POX), however, I do not seem to be able to find any information on how to make the request (using C#) and how to retrieve the response.

    Regards

    Andy

    Thursday, October 4, 2012 4:44 AM

Answers

  • You can use the Autodiscover classes to do an Autodiscover against your SCP endpoint (although I don't really see the point when you use AutoDiscoverURL it will do a SCP lookup first anyway?).

    eg to use the Autodiscover class you can use

                            adAutoDiscoverService.Url = new Uri(" https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc");
                            adAutoDiscoverService.Credentials = new NetworkCredential(UserName,Password);
                            adAutoDiscoverService.EnableScpLookup = false;
                            adAutoDiscoverService.RedirectionUrlValidationCallback = adAutoDiscoCallBack;
                            adAutoDiscoverService.PreAuthenticate = true;
                            GetUserSettingsResponse adResponse = adAutoDiscoverService.GetUserSettings(Identity, (new UserSettingName[1] { UserSettingName.ExternalEwsUrl }));
                            if (adResponse.Settings.Count >= 1)
                            {
                                String exClientURL = (String)adResponse.Settings[UserSettingName.ExternalEwsUrl];
                                CASURL = exClientURL;
                            }

    This will do a SOAP Autodiscover rather then POX if you want to do a POX discover you can do something like

    String EWSURL = null;
    String auDisXML = "<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\"><Request>" +
          "<EMailAddress>" + Identity + "</EMailAddress>" +
          "<AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>" +
          "</Request>" +
          "</Autodiscover>";
    System.Net.HttpWebRequest adAutoDiscoRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(adURL);
    adAutoDiscoRequest.ContentType = "text/xml";
    adAutoDiscoRequest.Headers.Add("Translate", "F");
    adAutoDiscoRequest.Method = "Post";
    adAutoDiscoRequest.Credentials = NetCredential;
    
    byte[] bytes = Encoding.UTF8.GetBytes(auDisXML);
    adAutoDiscoRequest.ContentLength = bytes.Length;
    Stream rsRequestStream = adAutoDiscoRequest.GetRequestStream();
    rsRequestStream.Write(bytes, 0, bytes.Length);
    rsRequestStream.Close();
    
    WebResponse adResponse = adAutoDiscoRequest.GetResponse();
    Stream rsResponseStream = adResponse.GetResponseStream();
    XmlDocument reResponseDoc = new XmlDocument();
    reResponseDoc.Load(rsResponseStream);
    String EWSURL = "";
    XmlNodeList pfProtocolNodes = reResponseDoc.GetElementsByTagName("Protocol");
    foreach (XmlNode xnNode in pfProtocolNodes)
    {
       
        XmlNodeList adChildNodes = xnNode.ChildNodes;
        foreach (XmlNode xnSubChild in adChildNodes)
        {
            switch (xnSubChild.Name)
                case "ASUrl": EWSURL = xnSubChild.InnerText;
                    break;
        }
    }
    Cheers
    Glen

    • Marked as answer by AndyW2007 Monday, October 8, 2012 6:04 PM
    Friday, October 5, 2012 6:35 AM

All replies

  • I don't think there is anything that is equivalent to mailbox AutoDiscover for discovering exchange servers.  The EWS service URL will always be "https://" + strExchangeServer + "/ews/exchange.asmx", where strExchangeServer is usually named remote.youremaildomainname     youremaildomainname is what comes after the @ sign for your email address.  strExchangeServer can also be an IP address.

    The autodiscover service was meant to give people a way to find mailboxes, as administrators often move them between physical machines when employees change their locations in a company.  Finding established exchange servers isn't as unpredictable. 

    Thursday, October 4, 2012 1:10 PM
  • You can use the Autodiscover classes to do an Autodiscover against your SCP endpoint (although I don't really see the point when you use AutoDiscoverURL it will do a SCP lookup first anyway?).

    eg to use the Autodiscover class you can use

                            adAutoDiscoverService.Url = new Uri(" https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc");
                            adAutoDiscoverService.Credentials = new NetworkCredential(UserName,Password);
                            adAutoDiscoverService.EnableScpLookup = false;
                            adAutoDiscoverService.RedirectionUrlValidationCallback = adAutoDiscoCallBack;
                            adAutoDiscoverService.PreAuthenticate = true;
                            GetUserSettingsResponse adResponse = adAutoDiscoverService.GetUserSettings(Identity, (new UserSettingName[1] { UserSettingName.ExternalEwsUrl }));
                            if (adResponse.Settings.Count >= 1)
                            {
                                String exClientURL = (String)adResponse.Settings[UserSettingName.ExternalEwsUrl];
                                CASURL = exClientURL;
                            }

    This will do a SOAP Autodiscover rather then POX if you want to do a POX discover you can do something like

    String EWSURL = null;
    String auDisXML = "<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\"><Request>" +
          "<EMailAddress>" + Identity + "</EMailAddress>" +
          "<AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>" +
          "</Request>" +
          "</Autodiscover>";
    System.Net.HttpWebRequest adAutoDiscoRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(adURL);
    adAutoDiscoRequest.ContentType = "text/xml";
    adAutoDiscoRequest.Headers.Add("Translate", "F");
    adAutoDiscoRequest.Method = "Post";
    adAutoDiscoRequest.Credentials = NetCredential;
    
    byte[] bytes = Encoding.UTF8.GetBytes(auDisXML);
    adAutoDiscoRequest.ContentLength = bytes.Length;
    Stream rsRequestStream = adAutoDiscoRequest.GetRequestStream();
    rsRequestStream.Write(bytes, 0, bytes.Length);
    rsRequestStream.Close();
    
    WebResponse adResponse = adAutoDiscoRequest.GetResponse();
    Stream rsResponseStream = adResponse.GetResponseStream();
    XmlDocument reResponseDoc = new XmlDocument();
    reResponseDoc.Load(rsResponseStream);
    String EWSURL = "";
    XmlNodeList pfProtocolNodes = reResponseDoc.GetElementsByTagName("Protocol");
    foreach (XmlNode xnNode in pfProtocolNodes)
    {
       
        XmlNodeList adChildNodes = xnNode.ChildNodes;
        foreach (XmlNode xnSubChild in adChildNodes)
        {
            switch (xnSubChild.Name)
                case "ASUrl": EWSURL = xnSubChild.InnerText;
                    break;
        }
    }
    Cheers
    Glen

    • Marked as answer by AndyW2007 Monday, October 8, 2012 6:04 PM
    Friday, October 5, 2012 6:35 AM
  • Hi Thanks,

    Once I had the SCP information I was able to do the POX request with code similar to the above. 

    Monday, October 8, 2012 6:04 PM
  • Hi 

    POX method works but when i decode the response i'm getting error code 600.

    How to get the entire response, i mean complete response like server url.

    Trying to achieve all this from a store app, so can't exchange API's

    Regards
    Varun


    Varun Ravindranath

    Thursday, January 23, 2014 6:22 PM