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

Export calendar Items to a CSV file using Microsoft Graph and Powershell

For the last couple of years the most constantly popular post by number of views on this blog has been  Export calendar Items to a CSV file using EWS and Powershell closely followed by the contact exports scripts. It goes to show this is just a perennial issue that exists around Mail servers, I think the first VBS script I wrote to do this type of thing was late 90's against Exchange 5.5 using cdo 1.2. Now it's 2020 and if your running Office365 you should really be using the Microsoft Graph API to do this. So what I've done is create a PowerShell Module (and I made it a one file script for those that are more comfortable with that format) that's a port of the EWS script above that is so popular. This script uses the ADAL library for Modern Authentication (which if you grab the library from the PowerShell gallery will come down with the module). Most EWS properties map one to one with the Graph and the Graph actually provides better information on recurrences then...

Downloading a shared file from Onedrive for business using Powershell

I thought I'd quickly share this script I came up with to download a file that was shared using One Drive for Business (which is SharePoint under the covers) with Powershell. The following script takes a OneDrive for business URL which would look like https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filename.txt This script is pretty simple it uses the SharePoint CSOM (Client side object Model) which it loads in the first line. It uses the URI object to separate the host and relative URL which the CSOM requires and also the SharePointOnlineCredentials object to handle the Office365 SharePoint online authentication. The following script is a function that take the OneDrive URL, Credentials for Office365 and path you want to download the file to and downloads the file. eg to run the script you would use something like ./spdownload.ps1 ' https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filen...

Sending a MimeMessage via the Microsoft Graph using the Graph SDK, MimeKit and MSAL

One of the new features added to the Microsoft Graph recently was the ability to create and send Mime Messages (you have been able to get Message as Mime for a while). This is useful in a number of different scenarios especially when trying to create a Message with inline Images which has historically been hard to do with both the Graph and EWS (if you don't use MIME). It also opens up using SMIME for encryption and a more easy migration path for sending using SMTP in some apps. MimeKit is a great open source library for parsing and creating MIME messages so it offers a really easy solution for tackling this issue. The current documentation on Send message via MIME lacks any real sample so I've put together a quick console app that use MSAL, MIME kit and the Graph SDK to send a Message via MIME. As the current Graph SDK also doesn't support sending via MIME either there is a workaround for this in the future my guess is this will be supported.
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.