2011-03-10 52 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

相关问题