Skip to main content

Cloud

Managed Metadata Hierarchy Breadcrumb

In my investigations of some of the new features of SharePoint 2010, I was interested to learn how I could programmatically walk the tree of a managed metadata hierarchy. This article walks through a program that will create a breadcrumb from a managed metadata hierarchy.
The first thing we need to do is to create the hierarchy. Being an avid boater, I thought I would create a hierarchy of boat types.
In Central Administration under Application Management, I clicked on Manage Service Applications.

Then click on the Managed Metadata Service link

Or select Managed Metadata Service from the list by clicking anywhere else on the line and click Manage in the Service Applications Tab in the ribbon.

Create a new group by selecting the dropdown arrow next to Managed Metadata Service in the Taxonomy Term Store in the left side panel.

You may need to add your account to the Term Store Administrators property in the right panel and click Save before seeing the dropdown arrow next to Managed Metadata Service.

I added the Boat Types term set by clicking the arrow next to the Boat Type group and selecting New Term Set.

I then proceeded to add new terms by clicking the arrow next the Boat Types term set and selecting Create Term.

Once you create a term, hit enter and the next blank term is available for entry.

There are various other selections if you select the arrow next to a term.

Below is the hierarchy I created for the Boat Type group.

Next, I created a new custom site column called Boat Type which has the Managed Metadata column type.

And selected Use a managed term set in the Term Set Settings then selected the Boat Types term set. Save the new site column.

Then I created new custom content type called River Trips using the Document Set Content Type.

I added the Boat Type site column to the River Trips content type.

In Advanced Settings, select ‘Yes’ in the Content Types setting to Allow management of Content Types.

Click on Document Set settings.

You can now set the Allowed Content Types, the Default Content when creating a new document set, the Shared Columns and the Welcome Page Columns for the document set.

I added the River Trips content type to the document library and deselected all other content types.
Now when you create a New Document,

You will be prompted for the Document Set metadata.

Selecting the icon next to the Boat Type input box will allow the selection of the Managed Metadata.

Or just begin typing and you will be prompted with the managed metadata terms. You can see a breadcrumb in the dropdown already.

If the term set submission policy is open then you can add new terms to the term set.

I added the following metadata for my Tennessee River trip.

And SharePoint created the document set with the shared metadata and default content types. I then edited the page and updated the default welcome image with my Tennessee River map. I also added the Visual web part below the map.

The code for the visual web part follows:
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

using Microsoft.Office.DocumentManagement.DocumentSets;
using Microsoft.SharePoint.Taxonomy;

namespace GenMMH.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public class VisualWebPart1 : WebPart
{
private const string _ascxPath = @”~/_CONTROLTEMPLATES/GenMMH/VisualWebPart1/VisualWebPart1UserControl.ascx”;

private string GenerateHierarchy()
{
string hierarchy = “”;
if (String.IsNullOrEmpty(System.Web.HttpContext.Current.Request.QueryString[“ID”]))
{
return hierarchy;
}
SPWeb mySite = SPContext.Current.Web;
TaxonomySession session = new TaxonomySession(mySite.Site);
string docId = System.Web.HttpContext.Current.Request.QueryString[“ID”];
int docSetId = Int32.Parse(docId);
SPList myList = SPContext.Current.List;
SPListItem listItem = myList.GetItemById(docSetId);
SPFolder myFolder = listItem.Folder;
DocumentSet myDocSet = Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.GetDocumentSet(myFolder);
//This column should not allow multiple values
TaxonomyFieldValue value = listItem[“Boat Type”] as TaxonomyFieldValue;

if (value != null)
{
Guid termGuid = new Guid(value.TermGuid);
Term term = session.GetTerm(termGuid);
if (term.Parent != null)
{
Term parentTerm = term.Parent;
//Assuming the taxonomy is not greater than 10 nodes deep.
string[] values = new String[10];
int count = -1;
while (parentTerm != null)
{
count++;
values[count] = parentTerm.Name;
parentTerm = parentTerm.Parent;
}
for (int i = count; i >= 0; i–)
{
hierarchy += values[i] + ” : “;
}
}
hierarchy += term.Name;
}
return hierarchy;
}

protected override void CreateChildControls()
{
Literal literal = new Literal();
literal.Text = GenerateHierarchy();
Controls.Add(literal);
}

}
}

References were added for Microsoft.Office.DocumentManagement.DocumentSets and Microsoft.SharePoint.Taxonomy
Notice that the ID field is used from the query string to identify the Document Set.
http://sp2010/River%20Trips/Forms/River%20Trips/docsethomepage.aspx?ID=5&FolderCTID=0x0120D520009CA21C1D2DBE5A4DA23B3BC785B7A11700225FEEF01FFD5242873D1E8E9B3948F5&List=ec36ffb1-1f70-4bd4-a270-7c85d5ec2453&RootFolder=%2FRiver%20Trips%2FTennessee%20River
Document Sets are SPFolders which allow inheritance of their metadata.
A new TaxonomySession is created for the site, the Boat Type column is identified as the TaxonomyFieldValue and the TermGuid of the TaxonomyFieldValue is used to get the metadata term. Now we start walking up the tree until there are no more parent terms. We then concatenate the terms with a semi-colon separator. Once completed, the hierarchy is added to the visual web part control as shown in the last screen capture below the map of my Tennessee River trip.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

David Sobiecki

More from this Author

Follow Us