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

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

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You need to authorize it in you tenant (eg build a small ap

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.