2011-03-10 33 views
6

根據發佈日期,我正在爲SharePoint 2010編寫一個webpart,用於恢復某個(自定義)類型的最新頁面。它只考慮標有特定術語的頁面。我希望它也可以在頁面上使用所選術語的子項進行標記。如何獲取C#中SharePoint術語的所有子項?

如果我有一個長期的樹,像這樣:

  • 英格蘭
    • 肯特
      • 坎特伯雷
    • 薩里
      • 鉻oydon
      • 克勞利

然後通過選擇肯特,我希望我的WebPart顯示標記肯特,多佛,或坎特伯雷最新的頁面。

這是可能的C#?

謝謝你的時間。

回答

4

功能搜索由ID術語庫的一個很好的例子,你要找的是Term.GetTerms

你需要從你的領域得到TaxonomyValue

然後,您必須獲取當前的TaxonomySession,然後使用TaxonomySession獲取該字段中使用的術語。從這個術語中,您可以使用父字段來獲取父字詞。 下面是一些粗略的代碼,向您展示所使用的對象。

  TaxonomyFieldValue v = null; // Notsurehowtodothisbit(); 
     TaxonomySession session = new TaxonomySession(site); 
     if (session.TermStores != null && session.TermStores.Count > 0) 
     { 

      TermStore termStore = session.TermStores[0]; 
      Term t = termStore.GetTerm(v.TermGuid); 
      Term parentTerm = t.Parent; 
      TermCollection childTerms = t.GetTerms(); 
     } 

一旦你的樹,你可以使用CAML查詢以生成SPList.GetList查詢帶回任何標記的方式。

我還沒有在這方面做過一個實驗... 但Bart-Jan Hoeijmakers

private SPListItemCollection GetItemsByTerm(Term term, SPList list) 
    { 
     // init some vars SPListItemCollection items = null;  
     SPSite site = SPContext.Current.Site;  // set up the TaxonomySession  
     TaxonomySession session = new TaxonomySession(site); 
     // get the default termstore TermStore termStore = session.TermStores[0]; 
     // If no wssid is found, the term is not used yet in the sitecollection, so no items exist using the term 
     int[] wssIds = TaxonomyField.GetWssIdsOfTerm(SPContext.Current.Site, termStore.Id, term.TermSet.Id, term.Id, false, 1); 
     if (wssIds.Length > 0) 
     { 
      // a TaxonomyField is a lookupfield. Constructing the SPQuery  
      SPQuery query = new SPQuery(); 
      query.Query = String.Format("<Where><Eq><FieldRef Name='MyTaxonomyField' LookupId='TRUE' /><Value Type='Lookup'>{0}</Value></Eq></Where>", wssIds[0]); 
      items = list.GetItems(query); 
     } 
     return items; 
    } 
+0

GetTerms是關鍵答案。 CAML適用於一個列表或內​​容查詢Web部件。查看我的更新答案,瞭解如何獲得初始術語guid以及在所有列表和庫中使用搜索的方法。 – 2011-03-11 15:12:53

2

納特的部分答案使用GetTerms方法的父母是偉大的。查詢一個列表的代碼看起來也不錯。

要獲得父項的標識,可以對標題使用TermStore.GetTerms。

要搜索網站集中的所有列表和庫,可以使用Search API的FullTextSQLQuery方法指定owstaxIdMyTaxonomyField作爲列的where子句中的guid。

有被標題得到的ID,並在Using taxonomy fields in SharePoint 2010: Part III

相關問題