Friday, June 14, 2013

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. }  


No comments: