Retrieving Contacts from EWS Managed API
I've researched ways to do this and looking into the API documentation with no luck. Does anyone know what is the necessa开发者_C百科ry code for me to retrieve the contacts for a specific user.
Any help will be greatly appreciated
All the other answers I found online are overly complicated. ResolveName should work.
The following example code worked for me; the rest is just looking into the different dictionaries and getting the necessary info out.
public void findContact(string name)
{
NameResolutionCollection coll = service.ResolveName(name,
ResolveNameSearchLocation.ContactsThenDirectory,
true);
foreach (NameResolution res in coll)
{
Contact contact = res.Contact;
if (contact != null)
{
// retrieve contact info here
if (contact.PhoneNumbers != null)
{
Console.WriteLine("get different kinds of phone numbers from here");
}
if (contact.EmailAddresses != null)
{
Console.WriteLine("get different kinds of email addresses from here");
}
return;
}
}
}
Basically, it boils down to these three lines:
var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1)
{Credentials = new NetworkCredential("username", "password")};
service.AutodiscoverUrl("targetmailaddress", url => true);
var items = service.FindItems(new FolderId(WellKnownFolderName.Contacts, new Mailbox("targetmailaddress")), new ItemView(512));
You need to replace targetmailaddress with the address you are looking for. If you only have one Exchange Server, you can omit the AutoDiscover part and add the service URL to the service initialization in the first list.
As usual, you can add an item filter to the FindItems call.
精彩评论