[英]How to get contact list from Exchange Server?
Can anyone tell me the simplest way to get a contact list from Exchange Server? 谁能告诉我从Exchange Server获取联系人列表的最简单方法? I'm using C#
我正在使用C#
From what I found out, Exchange Web Services only exists for Exchange Server 2007 and beyond. 根据我的发现, Exchange Web服务仅适用于Exchange Server 2007及更高版本。 That would be my first option, but I'd also like an alternative for previous versions of Exchange (WebDav or something).
这将是我的第一个选择,但我也想要替代以前版本的Exchange(WebDav或其他东西)。 Directory Services is not an option.
目录服务不是一种选择。
This is how to get the contact list from your contacts list in exchange using EWS. 这是如何使用EWS从您的联系人列表中获取联系人列表。 I'm not sure how to get contacts from the global list yet, only looked at the API an hour ago.
我不确定如何从全局列表中获取联系人,仅在一小时前查看了API。
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. 我已经无视创建verbocity服务,请查看Exchange Web服务API以获取更多信息。
First of all, don't forget to add a reference to the Microsoft Exchange Webservices Library. 首先,不要忘记添加对Microsoft Exchange Web服务库的引用。
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. ......和Brett Ryan已经提供了获取本地联系人列表的代码。
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. 这种检索全局联系人列表的方法(至少是其中一个)的问题在于,“ResolveName”函数最多可以返回100个联系人,这样如果您的组织有更多的记录,那么您就遇到了麻烦。 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.
一种可能的解决方法(以及我实现的解决方法)是对每个字母进行单独搜索(假设您可以验证此类搜索将始终返回少于100个结果)并将所有唯一条目串在一起列入一个列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.