Question

Can anyone tell me the simplest way to get a contact list from Exchange Server? I'm using C#

From what I found out, Exchange Web Services only exists for Exchange Server 2007 and beyond. That would be my first option, but I'd also like an alternative for previous versions of Exchange (WebDav or something). Directory Services is not an option.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top