首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用C#中的Exchange C#获取地址列表(而不是全局的)

如何使用C#中的Exchange C#获取地址列表(而不是全局的)
EN

Stack Overflow用户
提问于 2013-08-22 11:00:13
回答 2查看 1.1K关注 0票数 0

该网站显示了数十个例子,以查询交易所的全球地址列表,但我想查询特定的地址列表!因此,我们企业中的每个用户当然都被列在我们的全球地址列表中,但我想查询我们企业内部特定公司的地址列表。

在下面的例子中,Aswebo,Cosimco等等。是地址列表。

  1. 我如何列出这些地址列表?
  2. 我如何列出这些地址列表中的人?

EN

回答 2

Stack Overflow用户

发布于 2013-08-22 11:30:11

我没有exchange设置来测试这段代码,所以它需要修改,但是它应该给您一个起点来探索。

这样做的目的是将ItemView设置为ContactSchema,以便按公司检索结果。

代码语言:javascript
复制
// 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
}

您还可以使用service.FindItems(WellKnownFolderName、SearchFilter、ViewBase)提供额外的筛选。

有关代码示例,请参见这个MSDN博客

票数 1
EN

Stack Overflow用户

发布于 2013-08-22 15:45:32

我已经搜索了整个下午,并找到了下面的代码。起作用了..。但看上去很脏。我想要一套完整的校长方法,但我似乎太笨了:-)

有人想把这段代码翻译成100%的System.DirectoryServices.AccountManagement吗?

代码语言:javascript
复制
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();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18378590

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档