简体   繁体   English

如何在C#中使用Exchange Web服务获取地址列表(非全局)

[英]How do I get address lists (NOT global) using Exchange WebServices in C#

the web shows dozens of examples to query the exchange's global address list but i want to query the specific address lists! 网络上显示了数十个示例来查询交易所的全局地址列表,但我想查询特定的地址列表! So every user in our Enterprise is ofcourse listed in our global address list but i want to query the address list of a specific company within our Enterprise. 因此,我们企业中的每个用户当然都列在我们的全局地址列表中,但是我想查询企业中特定公司的地址列表。

In the example below, Aswebo, Cosimco, etc.. are address lists. 在下面的示例中,Aswebo,Cosimco等是地址列表。

  1. How do I list these address lists? 如何列出这些地址列表?
  2. How do I list the people within these address lists? 如何在这些地址列表中列出人员?

在此处输入图片说明

I don't have exchange setup to test this code, so it will need modifications but it should give you a starting point to explore. 我没有用于测试此代码的交换设置,因此需要进行修改,但它应该为您提供一个探索的起点。

The idea is that you set the ItemView to the ContactSchema to retrieve results by company. 想法是将ItemView设置为ContactSchema以按公司检索结果。

// 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
}

You can also use service.FindItems(WellKnownFolderName, SearchFilter, ViewBase) to provide additional filtering. 您还可以使用service.FindItems(WellKnownFolderName,SearchFilter,ViewBase)提供其他过滤。

See this MSDN blog for a code example. 有关代码示例,请参见此MSDN博客

I've been searching all afternoon and came up with the code below. 我一直在搜寻整个下午,并想出了以下代码。 It works.. but looking dirty. 它有效..但是看起来很脏 I would like an entire Principal approach but it seems I'm too dumb :-) 我想要一个完整的Principal方法,但是我似乎太傻了:-)

Anyone that wants to translate this code to 100% 'System.DirectoryServices.AccountManagement'? 是否要将此代码转换为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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# VSTO Outlook 插件 - 如何使用传出 Z0C83ZEFA57C7831CEB7B24 获取发送方的 email 地址? - C# VSTO Outlook plugin - How can I get the email address of the sender of an outgoing email using Exchange? 如何使用 c# 从全局地址列表中获取联系人 - How to get contacts from Global Address List using c# 使用不带附件的 Exchange Web 服务获取 Email 消息,使用 C#(在 UiPath 中) - Get Email Message using Exchange webservices without attachment, using C# (in UiPath) 如何使用C#与Exchange Server进行交互? - How do I interface with Exchange Server using C#? 如何使用 LINQ 在 C# 中处理子列表并获取新的 object 列表? - How do I handle child lists and get new object lists in C# using LINQ? 如何使用 Microsoft Exchange WebServices 搜索特定邮箱? - How do I search a specific mailbox with Microsoft Exchange WebServices? 我如何在C#中的两个列表中获得差异? - How do i get the difference in two lists in C#? C#,我如何从 TcpClient 获取 ip 地址? - C#, how do i get an ip address, from a TcpClient? 如何使用 c# 获取 mac os x 设备的物理地址 - How do i get Physical Address of a mac os x device by using c# 如何在Excel工作表中搜索特定文本并使用C#获取该文本占用的地址? - How do I search for specific text in an Excel worksheet and get the address that the text is occupying using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM