C# 如何使用C中的Exchange Web服务获取地址列表(非全局)#
网站上展示了几十个查询交易所全球地址列表的示例,但我想查询具体的地址列表!因此,我们企业中的每个用户当然都列在我们的全球地址列表中,但我想查询我们企业中特定公司的地址列表 在下面的示例中,Aswebo、Cosimco等。。是地址列表C# 如何使用C中的Exchange Web服务获取地址列表(非全局)#,c#,.net,visual-studio-2010,exchange-server,exchangewebservices,C#,.net,Visual Studio 2010,Exchange Server,Exchangewebservices,网站上展示了几十个查询交易所全球地址列表的示例,但我想查询具体的地址列表!因此,我们企业中的每个用户当然都列在我们的全球地址列表中,但我想查询我们企业中特定公司的地址列表 在下面的示例中,Aswebo、Cosimco等。。是地址列表 如何列出这些地址列表 如何列出这些地址列表中的人员 我没有exchange设置来测试这段代码,因此它需要修改,但应该给您一个探索的起点 其思想是将设置为按公司检索结果 // Get the number of items in the Contacts folder
我没有exchange设置来测试这段代码,因此它需要修改,但应该给您一个探索的起点 其思想是将设置为按公司检索结果
// Get the number of items in the Contacts folder. To keep the response smaller, request only the TotalCount property.
ContactsFolder contactsfolder = ContactsFolder.Bind(service,
WellKnownFolderName.Contacts,
new PropertySet(BasePropertySet.IdOnly, FolderSchema.TotalCount));
// Set the number of items to the smaller of the number of items in the Contacts folder or 1000.
int numItems = contactsfolder.TotalCount < 1000 ? contactsfolder.TotalCount : 1000;
// Instantiate the item view with the number of items to retrieve from the Contacts folder.
ItemView view = new ItemView(numItems);
view.PropertySet = new PropertySet(ContactSchema.CompanyName, ContactSchema.EmailAddress1);
// Retrieve the items in the Contacts folder that have the properties you've selected.
FindItemsResults<Item> contactItems = service.FindItems(WellKnownFolderName.Contacts, view);
foreach(var contact in contactItems)
{
Contact contact = item as Contact;
// Filter / Group by company name
// contact.Companyname
}
//获取联系人文件夹中的项目数。要使响应更小,请仅请求TotalCount属性。
ContactsFolder ContactsFolder=ContactsFolder.Bind(服务、,
WellKnownFolderName.联系人,
新的PropertySet(BasePropertySet.IdOnly,FolderSchema.TotalCount));
//将项目数设置为Contacts文件夹中项目数的较小值或1000。
int numItems=contactsfolder.TotalCount<1000?contactsfolder.TotalCount:1000;
//使用要从Contacts文件夹中检索的项目数实例化项目视图。
ItemView视图=新的ItemView(numItems);
view.PropertySet=newpropertyset(ContactSchema.CompanyName,ContactSchema.EmailAddress1);
//检索“联系人”文件夹中具有选定属性的项目。
FindItemsResults contactItems=service.FindItems(WellKnownFolderName.Contacts,视图);
foreach(contactItems中的var触点)
{
联系人=作为联系人的项目;
//按公司名称筛选/分组
//联系人:Companyname
}
您还可以使用提供附加筛选
请参阅以获取代码示例。我整个下午都在搜索,找到了下面的代码。它起作用了。。但是看起来很脏。我想要一个完整的主要方法,但似乎我太笨了:-) 有人想将此代码翻译成100%的“System.DirectoryServices.AccountManagement”吗
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
DirectoryEntry ldap;
DirectorySearcher ldap_search;
SearchResultCollection ldap_results;
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
var addressLists = new Dictionary<string, string>();
// Flexible way (but visually complex!) for building the path LDAP://CN=All Address Lists,CN=Address Lists Container,CN=First Organization,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=DOMAIN,DC=local
ldap = new DirectoryEntry("LDAP://RootDSE");
ldap_search = new DirectorySearcher(new DirectoryEntry("LDAP://CN=Microsoft Exchange, CN=Services," + ldap.Properties["configurationNamingContext"].Value), "(objectClass=msExchOrganizationContainer)");
ldap_search = new DirectorySearcher(new DirectoryEntry("LDAP://CN=All Address Lists,CN=Address Lists Container," + ldap_search.FindOne().Properties["distinguishedName"][0]), "(objectClass=addressBookContainer)");
ldap_search.Sort = new SortOption("name", SortDirection.Ascending);
// Find All Address Lists alphabetically and put these into a dictionary
ldap_results = ldap_search.FindAll();
foreach (SearchResult ldap_result in ldap_results)
{
var addressList = new DirectoryEntry(ldap_result.Path);
addressLists.Add(addressList.Properties["name"].Value.ToString(), addressList.Properties["distinguishedName"][0].ToString());
}
//// list Address Lists
//foreach (var addressList in addressLists) Console.WriteLine(addressList.Key);
// List all users from Address List "Aswebo"
ldap = new DirectoryEntry("LDAP://" + ldap.Properties["defaultNamingContext"].Value); // rename ldap to LDAP://DC=DOMAIN,DC=local
ldap_search = new DirectorySearcher(ldap, string.Format("(&(objectClass=User)(showInAddressBook={0}))", addressLists["Aswebo"])); // Search all users mentioned within the specified address list
ldap_results = ldap_search.FindAll();
foreach (SearchResult ldap_result in ldap_results)
{
// Fetch user properties using the newer interface.. just coz it's nice :-)
var User = UserPrincipal.FindByIdentity(ctx, IdentityType.DistinguishedName, ldap_result.Path.Replace("LDAP://", ""));
Console.WriteLine(User.DisplayName);
}
Console.ReadLine();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.DirectoryServices;
使用System.DirectoryServices.AccountManagement;
命名空间控制台应用程序3
{
班级计划
{
静态void Main(字符串[]参数)
{
目录条目ldap;
目录搜索器ldap_搜索;
SearchResultCollection ldap_结果;
PrincipalContext ctx=新PrincipalContext(ContextType.Domain);
var addressLists=新字典();
//构建路径LDAP://CN=All Address list,CN=Address list Container,CN=First Organization,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=DOMAIN,DC=local的灵活方式(但视觉上很复杂!)
ldap=newdirectoryEntry(“ldap://RootDSE”);
ldap_search=new DirectorySearcher(新目录条目(“ldap://CN=Microsoft Exchange,CN=Services,”+ldap.Properties[“configurationNamingContext”].Value),“(objectClass=MSExchorOrganizationContainer)”;
ldap_search=new DirectorySearcher(新的DirectoryEntry(“ldap://CN=All Address list,CN=Address list Container,”+ldap_search.FindOne().Properties[“differentiedname”][0]),“(objectClass=addressBookContainer)”;
ldap_search.Sort=新排序选项(“名称”,SortDirection.升序);
//按字母顺序查找所有地址列表,并将其放入字典
ldap_results=ldap_search.FindAll();
foreach(搜索结果ldap\u结果在ldap\u结果中)
{
var addressList=newdirectoryEntry(ldap\u result.Path);
addressList.Add(addressList.Properties[“name”].Value.ToString(),addressList.Properties[“DifferentizedName”][0].ToString());
}
////列表地址列表
//foreach(addressList中的var-addressList)Console.WriteLine(addressList.Key);
//从地址列表“Aswebo”中列出所有用户
ldap=newdirectoryentry(“ldap://”+ldap.Properties[“defaultNamingContext”].Value);//将ldap重命名为ldap://DC=DOMAIN,DC=local
ldap_search=new DirectorySearcher(ldap,string.Format((&(objectClass=User)(showInAddressBook={0})),AddressList[“Aswebo”]);//搜索指定地址列表中提到的所有用户
ldap_results=ldap_search.FindAll();
foreach(搜索结果ldap\u结果在ldap\u结果中)
{
//使用更新的界面获取用户属性..只是因为它很好:-)
var User=UserPrincipal.FindByIdentity(ctx,IdentityType.differentiedname,ldap_result.Path.Replace(“ldap:/”,“”));
Console.WriteLine(User.DisplayName);
}
Console.ReadLine();
}
}
}
1月,thx用于图形更新。我当时没有想到。谢谢你的回答。如果我理解正确,这种方法可能会奏效,但这需要循环浏览全球地址列表中的所有联系人,并将其保存为“公司”名称。因此,这不使用实际的地址列表。@TieleDeclercq您还可以使用它来提供额外的筛选,因此处理将在Exchange服务器上进行。有关示例,请参见。