Skip to content

Instantly share code, notes, and snippets.

@arjancornelissen
Created January 10, 2016 16:15
Show Gist options
  • Save arjancornelissen/696ca2f15d58ca16e63e to your computer and use it in GitHub Desktop.
Save arjancornelissen/696ca2f15d58ca16e63e to your computer and use it in GitHub Desktop.
Content type with Managed Metadata Feature receiver04
/// <summary>
/// Extension to search for a term in a Termset
/// </summary>
public static class TermExtension
{
/// <summary>
/// Search for the term by its path
/// </summary>
/// <param name="termset">The Termset where this path should be under</param>
/// <param name="path">The path to the term fron the Termset</param>
/// <returns>The <see cref="Term"/> it it's found</returns>
public static Term GetTermByPath(this TermSet termset, String path)
{
Term result = null;
string[] pathElements = path.Split(new Char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
int depth = 0;
try
{
result = termset.Terms[pathElements[depth]];
while (depth < pathElements.Length - 1)
{
depth++;
result = result.Terms[pathElements[depth]];
}
}
catch(Exception ex)
{
throw new ArgumentOutOfRangeException("Path", path, string.Format("Path not found; {0}", ex.Message));
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment