Thursday, November 3, 2011

Get Taxonomy Term by ID

static bool GetTermById(Guid idToMatch, TaxonomySession spTaxonomySession, ref Term result)
{

foreach (TermStore ts in spTaxonomySession.TermStores)
{
if (0 == ts.Id.CompareTo(idToMatch))
{
Console.WriteLine(" - matched TermStore: '" + ts.Name + "'");
return false;
}

foreach (Group group in ts.Groups)
{
if (0 == group.Id.CompareTo(idToMatch))
{
Console.WriteLine(" - matched Group: '" + group.Name + "'");
return false;
}
foreach (TermSet set in group.TermSets)
{
if (0 == set.Id.CompareTo(idToMatch))
{
Console.WriteLine(" - matched TermSet: '" + set.Name + "'");
return false;
}
foreach (Term term in set.Terms)
{
if (0 == idToMatch.CompareTo(term.Id))
{
result = term;
return true;
}
}
}
}
}
Console.WriteLine("FAILED - NOT FOUND");
return false;
}

No comments:

Post a Comment