Exporting taxonomies from a Microsoft SharePoint 2010 Term Store

Exporting taxonomies from a Microsoft SharePoint 2010 Term Store

Managing information is a critical part of all of our client engagements. As clients strive to get a balance of marketing elements such as product, placement, and promotion, certain keywords (metadata) can serve to enhance a content item’s findability. Key to improving the way customers find products is rich metadata management capabilities, like those in content management and search platforms such as SharePoint 2010.  But to effectively manage keywords in SharePoint 2010 Term Store, a basic capability is missing out of the box: being able to “own the data” by exporting the terms from Term Store Manager for use in a business’ collaborative processes.

SharePoint 2010 took a large step forward in taxonomy support with the introduction of the Term Store and Managed Metadata fields.  But what good is a taxonomy store if I can’t use my existing taxonomies while collaborating outside of SharePoint to manage and extend those taxonomies.  Half the problem is solved for you already.  There is a mechanism to import taxonomies through a comma-separated-value (CSV) file.  The supported format is basic but sufficient for many scenarios; each CSV file can contain a term set, its description, locale, and the hierarchy of terms.  You are able to specify the preferred labels of terms, their descriptions, and the hierarchical broader-narrower term relationships.

However the corresponding Export-to-CSV functionality is noticeably missing from the SharePoint Term Manager interface.  After some digging through the SharePoint Foundations Object Model, the method Microsoft.SharePoint.Taxonomy.Group.Export() turns up and would seem to provide exactly what we’re after.  The problem is this method is not implemented in the SharePoint 2010 release.  It may seem odd that a brand new API would ship with methods that are not implemented, but perhaps this is a placeholder for later implementation.

A custom solution is the only way to go for those looking to export terms to CSV files compatible with the existing Import-from-CSV functionality.  Fortunately it is straightforward to write a few lines of SharePoint Foundations Object Model code that will do the trick.  I wrote my export functionality within the context of a hosted WCF service that can be called remotely by any authenticated SharePoint user for a particular site but that is by no means necessary.  The export method outlined below will work under any context where SharePoint Foundations Object Model code would be expected to run.

There is still much room for improvement with this code.  Locale is not factored into the CSV export but that could be added easily.  The basic objective of a round trip through a CSV file is achieved with this method.  The next logical step would be to look at a richer import and export format, most likely based on XML.  We already need to implement the export half of the import-export equation with custom code.  There is no reason then not to choose a richer intermediate format than CSV that could support term reuse & id correlation, synonyms, localization, stakeholders, and just about any other metadata about the store, groups, term sets, and terms that would be useful for both import and export.

As with any application that interacts with the Term Store Object Model, you must start with a valid TaxonomySession object, created via a valid SPSite reference.  Since my application runs within a hosted WCF service, I am able to use the SPContext.Current property to access the target site.  Replace this bit of code with whatever mechanism you intend to use to obtain a SPSite reference, from there the algorithm is applicable regardless of how you’re connecting to the target site.  With just a few lines of code and judicious use of recursion we are able to quickly traverse down the term hierarchy, building the term path for each exported term, and writing it out to a string that can then be persisted to a CSV file.  For clarity sake, all exception and error handling has been removed.

The full C# source is attached.  Send me your comments and feedback about whether you found this useful, or how you’re solving the export problem.

using System;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
 
public class SimpleCSVExport
{
  public string ExportTermSet(string GroupName, string TermSetName)
  {
    StringBuilder exportData = new StringBuilder();
    exportData.AppendLine("\"Term Set Name\",\"Term Set Description\",\"LCID\",\"Available for Tagging\",\"Term Description\",\"Level 1 Term\",\"Level 2 Term\",\"Level 3 Term\",\"Level 4 Term\",\"Level 5 Term\",\"Level 6 Term\",\"Level 7 Term\"");
 
    TaxonomySession session = new TaxonomySession(SPContext.Current.Site);
    TermStore termStore = session.DefaultSiteCollectionTermStore;
    Group group = termStore.Groups[GroupName];
    TermSet termSet = group.TermSets[TermSetName];
 
    foreach (var topTerm in termSet.Terms)
    {
      exportData.AppendLine(String.Format("\"{0}\",\"{1}\",,{2},\"{3}\",\"{4}\",,,,,,,", termSet.Name, termSet.Description, termSet.IsAvailableForTagging, topTerm.GetDescription(), topTerm.Name));
 
      foreach (var childTerm in topTerm.Terms)
        ExportTerm(exportData, childTerm, String.Format("\"{0}\"", topTerm.Name));
    }
 
    return exportData.ToString();
  }
 
  private void ExportTerm(StringBuilder exportData, Term term, string heritage)
  {
    heritage = String.Concat(heritage, ",", String.Format("\"{0}\"", term.Name));
 
    exportData.AppendLine(String.Format(",,,{0},\"{1}\",{2}", term.IsAvailableForTagging, term.GetDescription(), heritage));
 
    foreach (var subTerm in term.Terms)
      ExportTerm(exportData, subTerm, heritage);
  }
}

Comments

Great article

Great post Corby. SharePoint 2010 is fantastic. There are many features. Very amazing and what is truly amazing is I host my sharepoint site with very low cost at http://www.asphostportal.com. It seems very good until now. You can go with this hosting provider. I see that this is the only provider which can offer sharepoint 2010 with very very low cost.

Sort Order

I think the CustomSortOrder get's lost with this export.

Great

Great post ... that´s what i missed in sharepoint 2010. Thx !!!

Getting Around Uploading to Server

Corby-
wondering if you could get the SharePoint Context, and not have to load code on the server by using a Silverlight app uploaded to the server to get your context.
Paul

Sort Order

I think the CustomSortOrder gets lost with this export.

custom sort order

It is true, ordering isn't taken into consideration as part of this exercise, but rather the basic steps to export a CSV file format that can be used in order to round-trip with the out-of-the-box CSV import experience.  We have been looking at a rich XML import/export format.  I would invite you to watch for future blogs on the topic in the coming weeks and you can even take a look at some of the early code efforts at http://taxoimport.codeplex.com/