English 中文(简体)
How to get contact list from Exchange Server?
原标题:

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.

最佳回答

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签