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
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
- NetworkCredential ncCred = new NetworkCredential("user@domain.onmicrosoft.com", "psword");
- String mbMailbox = "user@domain.onmicrosoft.com";
- AutodiscoverService adService = new AutodiscoverService(ExchangeVersion.Exchange2013);
- adService.Credentials = ncCred;
- adService.RedirectionUrlValidationCallback = adAutoDiscoCallBack;
- GetUserSettingsResponse adResponse = adService.GetUserSettings(mbMailbox, (new UserSettingName[2] { UserSettingName.ExternalOABUrl,UserSettingName.ExternalEwsUrl }));
- String exOABURL = (String)adResponse.Settings[UserSettingName.ExternalOABUrl];
- String ewsURL = (String)adResponse.Settings[UserSettingName.ExternalEwsUrl];
- String auDisXML = "";
- System.Net.HttpWebRequest oabRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create((exOABURL + "oab.xml"));
- byte[] bytes = Encoding.UTF8.GetBytes(auDisXML);
- oabRequest.ContentLength = bytes.Length;
- oabRequest.ContentType = "text/xml";
- oabRequest.Headers.Add("Translate", "F");
- oabRequest.Method = "GET";
- oabRequest.Credentials = ncCred;
- oabRequest.AllowAutoRedirect = false;
- WebResponse oabResponse = oabRequest.GetResponse();
- Stream rsResponseStream = oabResponse.GetResponseStream();
- XmlDocument reResponseDoc = new XmlDocument();
- reResponseDoc.Load(rsResponseStream);
- XmlNodeList oabDetails = reResponseDoc.GetElementsByTagName("OAL");
- String OabGuid = oabDetails[0].Attributes["dn"].Value.Substring(6);
- 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);
- ExchangeServiceBinding esb = new ExchangeServiceBinding();
- esb.Url = ewsURL;
- esb.Credentials = ncCred;
- esb.RequestServerVersionValue = new EWSProxy.RequestServerVersion();
- esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2013;
- FindPeopleType fpType = new FindPeopleType();
- IndexedPageViewType indexPageView = new IndexedPageViewType();
- indexPageView.BasePoint = IndexBasePointType.Beginning;
- indexPageView.Offset = 0;
- indexPageView.MaxEntriesReturned = 100;
- indexPageView.MaxEntriesReturnedSpecified = true;
- fpType.IndexedPageItemView = indexPageView;
- fpType.ParentFolderId = new TargetFolderIdType();
- DistinguishedFolderIdType contactsFolder = new DistinguishedFolderIdType();
- AddressListIdType adList = new AddressListIdType();
- adList.Id = OabGuid;
- fpType.ParentFolderId.Item = adList;
- FindPeopleResponseMessageType fpm = null;
- do
- {
- fpm = esb.FindPeople(fpType);
- if (fpm.ResponseClass == ResponseClassType.Success)
- {
- foreach (PersonaType PsCnt in fpm.People)
- {
- if (PsCnt.EmailAddress.MailboxTypeSpecified) {
- Console.WriteLine(PsCnt.EmailAddress.MailboxType);
- }
- Console.WriteLine( PsCnt.EmailAddress.EmailAddress);
- }
- indexPageView.Offset += fpm.People.Length;
- }
- else
- {
- throw new Exception("Error");
- }
- } while (fpm.TotalNumberOfPeopleInView > indexPageView.Offset);
- }
- internal static bool adAutoDiscoCallBack(string url)
- {
- return true;
- }