2010-02-09 11 views
5

Używam EWS i chcę uzyskać globalną listę adresów z wymiany dla firmy. Wiem, jak odzyskać osobistą listę kontaktów.Jak pobrać kontakty globalne za pomocą usług internetowych Exchange (EWS)?

Wszystkie próbki w dokumentacji interfejsu API dotyczą aktualizacji informacji o użytkowniku, ale nie mają konkretnego sposobu ich pobierania.

Próbowałem nawet następujących elementów, aby wyświetlić listę folderów, ale nie podniosło poprawnych wyników.

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) { 
      } 
     } 
    } 
} 

Choć kwestia została już podniesiona okazje (How to get contact list from Exchange Server?) To pytanie konkretnie z użyciem EWS dostać globalnej listy adresowej, podczas gdy kwestia ta prosi o radę na poziomie ogólnym.

Odpowiedz

2

was może dostał ItemType obiektów w specifiedfolder z fragmentem kodu poniżej a następnie rzucić ItemType sprzeciwia się ContactItemType (dla obiektów kontaktowych) ....

/// <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 ponieważ w rzeczywistości odbiera pytanie. Chcieliśmy tylko powtórzyć, że będzie to dotyczyło tylko użytkowników, którzy mają skrzynki pocztowe w Exchange, która jest podzbiorem użytkowników w AD. –

+0

Nie widzę, jak to odpowiada na pytanie? Brett wspomina, że ​​wie, jak wyciągnąć te informacje dla określonego folderu (kontakty), ale nie jak to zrobić dla globalnej listy adresów. – Miles

0

ja po prostu nie similiar rzecz. Nie udało mi się jednak uzyskać listy kontaktów za pośrednictwem programu Exchange, ponieważ pobiera ona tylko tych użytkowników, którzy mają skrzynki pocztowe, a niekoniecznie wszystkich użytkowników lub grupy. Ostatecznie otrzymałem wszystkich użytkowników poprzez AD

tutaj jest kod, aby uzyskać wszystkie kontakty w AD. Wszystko, czego potrzebujesz, to identyfikator folderu na globalnej liście adresów, który można pobrać z narzędzia ADSI.msc na serwerze AD i przejrzeć folder globalnej listy adresów, przejrzeć właściwości i pobrać wartość "rzekomego wyszukiwania". W moim systemie SearchPath dla globalnej listy adresów jest "(& (objectClass = user) (objectCategory = osoba) (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; 
    } 
Powiązane problemy