0
votes

I am trying to fetch all contacts (Global Address List & User Contacts Folder) using EWS.

Options tried till now :

  1. Service.ResolveName("SMTP:") - this has the limitation to fetch only first 100 characters as i wanted to get all contacts so that i can display in a grid along with paging. Also, turning on returnContactDetail = true (along with Property set specific to Contact Schema) does not return contact information like (display name, company name etc..)

    NameResolutionCollection nd = service.ResolveName("SMTP:", ResolveNameSearchLocation.ContactsThenDirectory, true, new PropertySet(BasePropertySet.IdOnly, new PropertyDefinitionBase[] { ContactSchema.ParentFolderId, ContactSchema.Id, ContactSchema.DisplayName, ContactSchema.EmailAddress1, ContactSchema.EmailAddress2, ContactSchema.EmailAddress3, ContactSchema.CompanyName }));

  2. service.FindItems(WellKnownFolderName.Contacts, new ItemView) - this only returns contacts from user's contacts folder not the global address list and also we need to resolve the email address using resolvename as emailadderess collection gives exchange formatted email address not in smtp format ([email protected]).

1

1 Answers

0
votes

The only other option in EWS is to use FindPeople operation https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/findpeople-operation if you know the GUID of the Global Address list you should be able to page the whole thing back eg .

    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 = "2117949e-abe8-4915-91eb-6b9f867fd8de";  

    fpType.ParentFolderId.Item = adList;  
    FindPeopleResponseMessageType fpm = null;  
    do  
    {  
        fpm = esb.FindPeople(fpType);  
        if (fpm.ResponseClass == ResponseClassType.Success)  
        {  
            foreach (PersonaType PsCnt in fpm.People) {  
                Console.WriteLine(PsCnt.EmailAddress.EmailAddress);  
            }  
            indexPageView.Offset += fpm.People.Length;                      
        }  
        else {  
            throw new Exception("Error");  
        }  
    } while (fpm.TotalNumberOfPeopleInView > indexPageView.Offset);  

Otherwise consider not using EWS and use the Directory directly eg if its onpremise use LDAP via System.DirectoryServices or if its Office365 then you can use the Graph to access all the directory objects.