How to get contact list from Exchange Server?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 21:28:58

This is how to get the contact list from your contacts list in exchange using EWS. I'm not sure how to get contacts from the global list yet, only looked at the API an hour ago.

private static void ListContacts(ExchangeService svc) {
    foreach (var v in svc.FindItems(WellKnownFolderName.Contacts,
                                    new ItemView(20))) {
        Contact contact = v as Contact;
        ContactGroup contactGroup = v as ContactGroup;

        //v.Load(); // Turns out you don't need to load for basic props.
        if (contact != null) {
            Console.WriteLine("Contact: {0} <{1}>",
                contact.DisplayName,
                contact.EmailAddresses[EmailAddressKey.EmailAddress1]);
        } else if (contactGroup != null) {
            Console.WriteLine("Contact Group: {0}", contactGroup.DisplayName);
            switch (svc.RequestedServerVersion) {
                case ExchangeVersion.Exchange2007_SP1:
                    ExpandGroupResults groupResults
                        = svc.ExpandGroup((contactGroup.Id));
                    foreach (var member in groupResults) {
                        Console.WriteLine("+ {0} <{1}>",
                            member.Name, member.Address);
                    }
                    break;
                case ExchangeVersion.Exchange2010:
                    foreach (GroupMember member in contactGroup.Members) {
                        Console.WriteLine("+ {0} <{1}>",
                        member.AddressInformation.Name,
                        member.AddressInformation.Address);
                    }
                    break;
                default:
                    Console.WriteLine(
                        "** Unknown Server Version: {0}",
                        svc.RequestedServerVersion);
                    break;
            }
        } else {
            Console.WriteLine("Unknown contact type: {0} - {1}",
                contact.GetType(), v.Subject);
        }
    }
}

I've ommited creating the service for verbocity, have a look at the Exchange Web Services API for more information.

First of all, don't forget to add a reference to the Microsoft Exchange Webservices Library.

private static void ConnectToExchangeService()
{
    service = new ExchangeService(); 
    service.Credentials = new WebCredentials(USERNAME, PASSWORD, DOMAIN_NAME);
    service.AutodiscoverUrl(USER_ADDRESS);
}

private static void ListGlobalContacts(ExchangeService service)
{
    /* passing true as the third parameter to "ResolveName" is important to
       make sure you get the contact details as well as the mailbox details */
    NameResolutionCollection searchResult = service.ResolveName(NAME_YOURE_LOOKING_FOR, ResolveNameSearchLocation.DirectoryOnly, true);
    foreach (NameResolution resolution in searchResult )
    {
        Console.WriteLine("name is " + resolution.Contact.DisplayName);
        Console.WriteLine("address is " + resolution.Mailbox.Address);
        Console.WriteLine("business phone is " + resolution.Contact.PhoneNumbers[PhoneNumberKey.BusinessPhone]);
        Console.WriteLine("mobile phone is " + resolution.Contact.PhoneNumbers[PhoneNumberKey.MobilePhone]);
    }
}

...and Brett Ryan already supplied the code for getting the local contacts list.

The problem with this method of retrieving the global contacts list (well, one of them at least) is that the function "ResolveName" returns up to 100 contacts so that if your organization has more records than that, you're in trouble. One possible workaround (and the one I implemented) is to conduct a seperate search for each letter (assuming you can verify that such a search will always return less than 100 results) and string all the unique entries together into one list.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!