2010-01-15 58 views
14

任何人都可以告訴我從Exchange Server獲取聯繫人列表的最簡單方法嗎?我正在使用C#如何從Exchange Server獲取聯繫人列表?

從我發現的情況來看,Exchange Web服務僅適用於Exchange Server 2007及更高版本。這將是我的第一選擇,但我也想要以前版本的Exchange(WebDav或其他)的替代品。目錄服務不是一個選項。

+0

是EWS託管API(.Net 3.5 pre-req)的一個選項嗎? – 2010-01-18 13:14:35

+0

阿爾弗雷德,我一直在調查一下,並且由於根據版本有不同的方式連接到Exchange,現在暫停。 但是,我的第一個選擇是EWS託管API,是的。 謝謝! – Johnny 2010-01-20 13:18:03

+0

我之前爲Exchange版本所做的工作很簡單,就是從主動目錄中檢索列表。 – 2010-02-10 21:46:50

回答

11

這是如何從聯繫人列表中使用EWS交換聯繫人列表。我不確定如何從全局列表中獲取聯繫人,但僅在一小時前查看過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); 
     } 
    } 
} 

我中省略創造verbocity服務,看看在Exchange Web Services API以獲取更多信息。

+0

我發現與託管API一起安裝的GettingStarted.doc實際上相當有助於建立服務參考 – 2012-01-17 15:03:35

1

首先,不要忘記添加對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]); 
    } 
} 

...... Brett Ryan已經提供了獲取本地聯繫人列表的代碼。

這種檢索全局聯繫人列表(至少其中之一)的方法存在的問題是,函數「ResolveName」最多返回100個聯繫人,因此如果您的組織擁有的記錄多於此,麻煩。一種可能的解決方法(以及我實現的一種)是對每個字母進行單獨搜索(假設您可以驗證這樣的搜索總是返回少於100個結果),並將所有唯一條目串聯到一個列表中。

相關問題