功能搜索由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;
}
来源
2011-03-11 03:31:53
Nat
GetTerms是关键答案。 CAML适用于一个列表或内容查询Web部件。查看我的更新答案,了解如何获得初始术语guid以及在所有列表和库中使用搜索的方法。 – 2011-03-11 15:12:53