Displaying your Sharepoint taxonomy in a visual web part for easy reference.

Thu 21 June 2012

I was reading a post by Tobias Zimmergren on how to render your sharepoint taxonomy in a Visual Web Part (great article, thanks Tobias!) and decided to try it out for a client Sharepoint; it worked rather well for small taxonomies, but I started seeing a few issues when working with larger taxonomies that had multiple levels of terms. Rather than scrap the whole thing, I figured that I would re-write it a bit and see what happened.

The resulting code worked great for me, so I'm publishing it in hopes that someone keeps improving on Tobias' original work. The reason why I had sought out something like this is because one often has to re-work taxonomies and the de-facto Term Manager doesn't output the taxonomy in a very printer-friendly format -- this definitely helps!

To use it, open up Visual Studio and create a new MOSS 2010 Visual Web Part project. Your Sharepoint Visual Web Part will have a TreeView control on it called tvMetaDataTree; its code will look something like this:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;

namespace DisplayTaxonomy.VisualWebPart1{

public partial class VisualWebPart1UserControl : UserControl {

    protected void Page\_Load(object sender, EventArgs e) {
        TaxonomyToTreeSet taxonomyHelper = new TaxonomyToTreeSet();
        tvMetadataTree.Nodes.Add(taxonomyHelper.getTreeTaxonomy());
        }

    }

}

Your taxonomy-to-treeview class will be a bit heftier; I've implemented it using recursive programming because to me it seems simpler and more efficient (though more resource-consuming)

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using System.Collections;

namespace DisplayTaxonomy {

    class TaxonomyToTreeSet {

        TaxonomySession _TaxonomySession;
        TreeNode _RootNode;

        public TaxonomyToTreeSet() {

            SPSite thisSite = SPContext.Current.Site;
            _TaxonomySession = new TaxonomySession(thisSite);
            _RootNode = new TreeNode();
            _RootNode.Text = "Site Taxonomy";
            getTermStores(_TaxonomySession, ref _RootNode);

        }

        public TreeNode getTreeTaxonomy() {
            return _RootNode;
        }

        private void getTermStores(TaxonomySession session, ref TreeNode parent) {

            foreach (TermStore ts in session.TermStores) {

                TreeNode node = new TreeNode(ts.Name, null, null, "", null);
                getGroups(ts, ref node);
                parent.ChildNodes.Add(node);

                }
        }

        private void getGroups(TermStore ts, ref TreeNode parent)

        {

            foreach (Group g in ts.Groups) {
                TreeNode node = new TreeNode(g.Name, null, null, "", null);
                getTermSets(g, ref node);
                parent.ChildNodes.Add(node);

            }

        }

        private void getTermSets(Group g, ref TreeNode parent) {

            foreach (TermSet tset in g.TermSets) {

                TreeNode node = new TreeNode(tset.Name, null, null, "", null);
                getTerms(tset, ref node);
                parent.ChildNodes.Add(node);

            }

        }

        private void getTerms(object term, ref TreeNode parent) {

            if(term.GetType() == typeof(Term)) {

                foreach (Term t in ((Term)term).Terms) {

                    TreeNode node = new TreeNode(t.Name, null, null, "", null);
                    getTerms(t, ref node);
                    parent.ChildNodes.Add(node);

                }

            }

            if (term.GetType() == typeof(TermSet)) {

                foreach (Term t in ((TermSet)term).Terms) {

                    TreeNode node = new TreeNode(t.Name, null, null, "", null);
                    getTerms(t, ref node);
                    parent.ChildNodes.Add(node);

                }

            }

        }

    }

}

That pretty much covers it, really. I hope that you find this useful. Don't hesitate to share! If you would rather see a video demo of how to implement this drop me a comment and I'll set that up.

Happy coding,

Rick.

blogroll

social