Skip to main content

EWS Findpeople workaround for reading the Offline Address Book in Office365 / Exchange Online

A couple of weeks ago I posted this about using the new FindPeople operation in EWS with Exchange 2013 to enumerate through the Global Address List. As I mentioned one pain point around using this new operation on Office365 is that you need to know the AddressList Id which there is no way of dynamically getting via EWS. This has been bugging me for a while so I started thinking about some ways of working around this and one method I found that did work is you can obtain the Id for the Offline Address Book and then query this (which is mostly as good as querying the Online GAL).

To Get the Id of the Offline Address book what you first need to do is use AutoDiscover to get the External OAB url, Then use a normal Get request on this url for the oab.xml file. Then you can parse from the OAB.xml file the Guid value of the OAB which you can transform into an AddressList id that you can then use with EWS to query the OAB. The following C# sample use the EWS Managed API for Autodiscover and then use some WSDL Proxy code to execute the FindPeople Operation

  1.     NetworkCredential ncCred = new NetworkCredential("user@domain.onmicrosoft.com""psword");  
  2.     String mbMailbox = "user@domain.onmicrosoft.com";  
  3.     AutodiscoverService adService = new AutodiscoverService(ExchangeVersion.Exchange2013);  
  4.     adService.Credentials = ncCred;  
  5.     adService.RedirectionUrlValidationCallback = adAutoDiscoCallBack;  
  6.     GetUserSettingsResponse adResponse = adService.GetUserSettings(mbMailbox, (new UserSettingName[2] { UserSettingName.ExternalOABUrl,UserSettingName.ExternalEwsUrl }));  
  7.     String exOABURL = (String)adResponse.Settings[UserSettingName.ExternalOABUrl];  
  8.     String ewsURL = (String)adResponse.Settings[UserSettingName.ExternalEwsUrl];  
  9.     String auDisXML = "";  
  10.     System.Net.HttpWebRequest oabRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create((exOABURL + "oab.xml"));  
  11.   
  12.      
  13.     byte[] bytes = Encoding.UTF8.GetBytes(auDisXML);  
  14.     oabRequest.ContentLength = bytes.Length;  
  15.     oabRequest.ContentType = "text/xml";  
  16.     oabRequest.Headers.Add("Translate""F");  
  17.     oabRequest.Method = "GET";  
  18.     oabRequest.Credentials = ncCred;  
  19.     oabRequest.AllowAutoRedirect = false;  
  20.     WebResponse oabResponse = oabRequest.GetResponse();  
  21.   
  22.     Stream rsResponseStream = oabResponse.GetResponseStream();  
  23.     XmlDocument reResponseDoc = new XmlDocument();  
  24.     reResponseDoc.Load(rsResponseStream);  
  25.     XmlNodeList oabDetails = reResponseDoc.GetElementsByTagName("OAL");  
  26.     String OabGuid = oabDetails[0].Attributes["dn"].Value.Substring(6);  
  27.     OabGuid =  OabGuid.Substring(6, 2) + OabGuid.Substring(4, 2) + OabGuid.Substring(2, 2) + OabGuid.Substring(0, 2) + "-" + OabGuid.Substring(10, 2) + OabGuid.Substring(8, 2) + "-" + OabGuid.Substring(14, 2) + OabGuid.Substring(12, 2) + "-" + OabGuid.Substring(16, 4) + "-" + OabGuid.Substring(20, 12);  
  28.   
  29.   
  30.     ExchangeServiceBinding esb = new ExchangeServiceBinding();  
  31.     esb.Url = ewsURL;  
  32.     esb.Credentials = ncCred;  
  33.     esb.RequestServerVersionValue = new EWSProxy.RequestServerVersion();  
  34.     esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2013;  
  35.   
  36.     FindPeopleType fpType = new FindPeopleType();  
  37.     IndexedPageViewType indexPageView = new IndexedPageViewType();  
  38.     indexPageView.BasePoint = IndexBasePointType.Beginning;  
  39.     indexPageView.Offset = 0;  
  40.     indexPageView.MaxEntriesReturned = 100;  
  41.     indexPageView.MaxEntriesReturnedSpecified = true;  
  42.     fpType.IndexedPageItemView = indexPageView;  
  43.   
  44.   
  45.     fpType.ParentFolderId = new TargetFolderIdType();  
  46.     DistinguishedFolderIdType contactsFolder = new DistinguishedFolderIdType();  
  47.     AddressListIdType adList = new AddressListIdType();  
  48.     adList.Id = OabGuid;  
  49.   
  50.     fpType.ParentFolderId.Item = adList;  
  51.     FindPeopleResponseMessageType fpm = null;  
  52.     do  
  53.     {  
  54.         fpm = esb.FindPeople(fpType);  
  55.         if (fpm.ResponseClass == ResponseClassType.Success)  
  56.         {  
  57.             foreach (PersonaType PsCnt in fpm.People)  
  58.             {  
  59.                 if (PsCnt.EmailAddress.MailboxTypeSpecified) {  
  60.                     Console.WriteLine(PsCnt.EmailAddress.MailboxType);  
  61.                 }  
  62.                 Console.WriteLine( PsCnt.EmailAddress.EmailAddress);  
  63.             }  
  64.             indexPageView.Offset += fpm.People.Length;  
  65.         }  
  66.         else  
  67.         {  
  68.             throw new Exception("Error");  
  69.         }  
  70.     } while (fpm.TotalNumberOfPeopleInView > indexPageView.Offset);    
  71.   
  72.   
  73. }  
  74. internal static bool adAutoDiscoCallBack(string url)  
  75. {  
  76.     return true;  
  77. }  


Popular posts from this blog

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.