2010-02-09 30 views
5

Tôi đang sử dụng EWS và muốn lấy danh sách địa chỉ toàn cầu để đổi lấy công ty. Tôi biết cách truy xuất danh sách liên hệ cá nhân.Làm cách nào để truy xuất danh bạ chung với Exchange Web Services (EWS)?

Tất cả các mẫu trong tài liệu API đối phó với việc cập nhật thông tin người dùng nhưng không cụ thể cách truy xuất chúng.

Tôi thậm chí đã cố gắng sau đây để liệt kê các thư mục nhưng nó không yeild kết quả chính xác.

private static void ListFolder(ExchangeService svc, FolderId parent, int depth) { 
    string s; 
    foreach (var v in svc.FindFolders(parent, new FolderView(int.MaxValue))) { 
     Folder f = v as Folder; 
     if (f != null) { 
      s = String.Format("[{0}]", f.DisplayName); 
      Console.WriteLine(s.PadLeft(s.Length + (depth * 2))); 
      ListFolder(svc, f.Id, depth + 1); 

      try { 
       foreach (Item i in f.FindItems(new ItemView(20))) { 
        Console.WriteLine(
         i.Subject.PadLeft(i.Subject.Length + ((depth + 1) * 2))); 
       } 
      } catch (Exception) { 
      } 
     } 
    } 
} 

Trong khi câu hỏi đã được nêu ra giao dịch (How to get contact list from Exchange Server?) câu hỏi này đặc biệt với việc sử dụng EWS để có được danh sách địa chỉ toàn cầu trong khi câu hỏi này hỏi để được tư vấn về mặt kĩ nói chung.

Trả lời

2

bạn có thể nhận được itemtype các đối tượng trong một specifiedfolder với đoạn mã dưới đây và sau đó đúc itemtype đối tượng để ContactItemType (đối với đối tượng tiếp xúc) ....

/// <summary> 
    /// gets list of ItemType objects with maxreturncriteria specicification 
    /// </summary> 
    /// <param name="esb">ExchangeServiceBinding object</param> 
    /// <param name="folder">FolderIdType to get items inside</param> 
    /// <param name="maxEntriesReturned">the max count of items to return</param> 
    public static List<ItemType> FindItems(ExchangeServiceBinding esb, FolderIdType folder, int maxEntriesReturned) 
    { 
     List<ItemType> returnItems = new List<ItemType>(); 
     // Form the FindItem request 
     FindItemType request = new FindItemType(); 
     request.Traversal = ItemQueryTraversalType.Shallow; 
     request.ItemShape = new ItemResponseShapeType(); 
     request.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties; 
     request.ParentFolderIds = new FolderIdType[] { folder }; 
     IndexedPageViewType indexedPageView = new IndexedPageViewType(); 
     indexedPageView.BasePoint = IndexBasePointType.Beginning; 
     indexedPageView.Offset = 0; 
     indexedPageView.MaxEntriesReturned = 100; 
     indexedPageView.MaxEntriesReturnedSpecified = true; 
     request.Item = indexedPageView; 
     FindItemResponseType response = esb.FindItem(request); 
     foreach (FindItemResponseMessageType firmtMessage in response.ResponseMessages.Items) 
     { 
      if (firmtMessage.ResponseClass == ResponseClassType.Success) 
      { 
       if (firmtMessage.RootFolder.TotalItemsInView > 0) 
        foreach (ItemType item in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items) 
         returnItems.Add(item); 
         //Console.WriteLine(item.GetType().Name + ": " + item.Subject + ", " + item.DateTimeReceived.Date.ToString("dd/MM/yyyy")); 
      } 
      else 
      { 
      //handle error log here 
      } 
     } 
     return returnItems; 
    } 
+0

+1 vì bạn đang thực sự trả lời của mình câu hỏi. Mặc dù chỉ muốn lặp lại rằng điều này sẽ chỉ lấy người dùng có hộp thư trong Exchange là một tập con của người dùng trong AD. –

+0

Tôi không thấy cách này trả lời câu hỏi? Brett đề cập rằng anh ta biết cách kéo thông tin này cho một thư mục cụ thể (Danh bạ) nhưng không biết cách thực hiện nó cho Danh sách địa chỉ chung. – Miles

0

tôi chỉ làm một điều tương tự. Tuy nhiên, tôi không thể nhận danh sách liên hệ qua Exchange vì chỉ có người dùng có hộp thư và không nhất thiết phải là tất cả người dùng hoặc nhóm. Cuối cùng tôi đã nhận được tất cả người dùng qua AD

đây là mã để nhận tất cả địa chỉ liên hệ trong AD. Tất cả những gì bạn cần là folderID của danh sách địa chỉ chung có thể nhận được từ việc sử dụng công cụ ADSI.msc trên máy chủ AD của bạn và duyệt đến thư mục Global address list, xem các thuộc tính và lấy giá trị của "search purported". Trong hệ thống của tôi searchPath cho danh sách địa chỉ toàn cầu là "(& (objectClass = user) (objectCategory = người) (mailNickname = ) (msExchHomeServerName =))"

public List<ListItem> SearchAD(string keyword, XmlDocument valueXml) 
    {   
     List<ListItem> ewsItems = new List<ListItem>(); 

     using (DirectoryEntry ad = Utils.GetNewDirectoryEntry("LDAP://yourdomain.com")) 
     { 
      Trace.Info("searcherをつくる"); 
      using (DirectorySearcher searcher = new DirectorySearcher(ad)) 
      { 
       if (this.EnableSizeLimit) 
       {   
        searcher.SizeLimit = GetMaxResultCount(); 

        if (Utils.maxResultsCount > 1000) 
        { 
         searcher.PageSize = 100; 
        } 
       } 
       else 
       { 
        searcher.SizeLimit = 1000; 
        searcher.PageSize = 10; 
       } 

       string sisya = Utils.DecodeXml(valueXml.SelectSingleNode("Folder/SearchPath").InnerText); //this is the folder to grab your contacts from. In your case Global Address list 

       //Container 
       if(String.IsNullOrEmpty(sisya)) 
       { 
        return null; 
       } 

       keyword = Utils.EncodeLdap(keyword); 

       string text = Utils.DecodeXml(valueXml.SelectSingleNode("Folder/Text").InnerText); 

       searcher.Filter = this.CreateFilter(keyword, sisya); 
       searcher.Sort = new SortOption("DisplayName", System.DirectoryServices.SortDirection.Ascending); 

       //一つのPropertyをロードすると、全Propertyを取らないようになる 
       searcher.PropertiesToLoad.Add("SAMAccountName"); //どのPropertyでもいい。 


       SearchResultCollection searchResults = searcher.FindAll(); 



       foreach (SearchResult searchResult in searchResults) 
       { 
        //ListItem contact = null; 
        using (DirectoryEntry userEntry = searchResult.GetDirectoryEntry()) 
        { 
         try 
         { 
          string schemaClassName = userEntry.SchemaClassName; 
          switch (schemaClassName) 
          { 
           case "user": 
           case "contact": 
            string fname = userEntry.Properties["GivenName"].Value == null ? "" : userEntry.Properties["GivenName"].Value.ToString(); 
            string lname = userEntry.Properties["sn"].Value == null ? "" : userEntry.Properties["sn"].Value.ToString(); 
            string dname = userEntry.Properties["DisplayName"][0] == null ? lname + " " + fname : userEntry.Properties["DisplayName"][0].ToString(); 

            //No Mail address 
            if ((userEntry.Properties["mail"] != null) && (userEntry.Properties["mail"].Count > 0)) 
            { 


             string sAMAccountName = ""; 
             if(userEntry.Properties["SAMAccountName"].Value != null){ 
              sAMAccountName = userEntry.Properties["SAMAccountName"].Value.ToString(); 
             } 
             else{ 
              sAMAccountName = userEntry.Properties["cn"].Value.ToString(); 
             } 
             string contactXml = Utils.ListViewXml(sAMAccountName, UserType.User, Utils.UserXml(fname, lname, userEntry.Properties["mail"].Value.ToString(), dname, null), ServerType.Ad); 
             ewsItems.Add(new ListItem(dname + "<" + userEntry.Properties["mail"].Value.ToString() + ">", contactXml)); 
            } 
            else 
            { 
             ListItem contact = new ListItem(dname, null); 
             contact.Enabled = false; 

             ewsItems.Add(contact); 

             Trace.Info("追加できないユーザ: " + searchResult.Path); 
            } 
            break; 
           case "group": 
            ewsItems.Add(new ListItem(userEntry.Properties["DisplayName"].Value.ToString(), Utils.ListViewXml(userEntry.Properties["SAMAccountName"].Value.ToString(), UserType.Group, null, ServerType.Ad))); 
            break; 
           default: 
            userEntry.Properties["SAMAccountName"].Value.ToString()); 
            ewsItems.Add(new ListItem(userEntry.Properties["name"].Value.ToString(), Utils.ListViewXml(userEntry.Properties["SAMAccountName"].Value.ToString(), UserType.Group, null, ServerType.Ad))); 
            break; 

          } 
         } 
         catch (Exception ex) 
         { 
          Trace.Error("User data取得失敗", ex); 
         } 
        } 
       } 

       searchResults.Dispose(); 

      } 
     }  
     return ewsItems; 
    } 
Các vấn đề liên quan