Second Life of a Hungarian SharePoint Geek

February 10, 2010

Getting Term by the path in the taxonomy hierarchy

Filed under: SP 2010, Taxonomies — Tags: , — Peter Holpar @ 18:06

I’ve not found any built-in method to get a specific Term in the taxonomy hierarchy by specifying its path. I mean the name of the parent terms from the root TermSet and the name of the term itself, like “parent of the parent term name/parent term name/term name”. Terms having the same name may exist at multiple levels or branches of the taxonomy, so the name of the term is not necessarily unique within a TermSet, one can not use that to look up the exact Term.

So I’ve created an extension method that does just what I need, here is the code:

  1. public static class Helper
  2. {
  3.     public static Term GetTermByPath(this TermSet termSet, String path)
  4.     {
  5.         Term result = null;
  7.         String[] pathElements = path.Split(new Char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
  9.         int depth = 0;
  10.         result = termSet.Terms[pathElements[depth]];
  12.         while (depth < pathElements.Length-1)
  13.         {
  14.             depth++;
  15.             result = result.Terms[pathElements[depth]];
  16.         }
  18.         return result;
  19.     }
  20. }

In the following code we assume a term set hierarchy that was imported from the sample import CSV as I’ve discussed in a former post.

  1. TaxonomySession session = new TaxonomySession(site);
  2. TermStore termStore = session.TermStores["Managed Metadata Service"]; // replace it to fit your configuration
  3. Group group = termStore.Groups["GroupName"]; // replace it to fit your configuration
  5. TermSet termSet = group.TermSets["Political Geography"];
  6. Term term = termSet.GetTermByPath("Continent/Political Entity/Country/Province or State/County or Region/City/District");

As usually the codes above are only for illustration purposes. If you would like to use them in a production code, you should add some kind of exception handling to them.

And please, let me know if you find this method in the standard API!



  1. […] the GetTermByPath method I used to get the value of the AnchorId property see my former post. Anchor is a term that refers to the Term that is the starting point of the “navigation” within […]

    Pingback by Adding a Managed Metadata Column to a list via SharePoint Server 2010 object model « Second Life of a Hungarian SharePoint Geek — February 11, 2010 @ 16:24

  2. Hi,

    I’m exporting user profile properties to xml and would like to export taxonomy if the property has a termset. I can get the termset like this:
    How do I get parent group or groups of this termset?


    Comment by Joe — December 8, 2010 @ 16:06

    • Hi Joe,

      See the definition of the TermSet class. It contains a property called Group that returns the parent group for the term set.


      Comment by Peter Holpar — December 21, 2010 @ 22:21

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: