sharepoint Create metadata term with powershell with guidWhether you are using SharePoint 2010 or SharePoint 2013, it is very likely you are taking advantage of the Managed Metadata service for taxonomy and other purposes.  Hopefully you have a development / test environment in addition to production, and if so you know it’s a struggle to keep them in sync.  If your groups, term sets and terms are not identical down to GUIDs between the two environments, if you move a database from one to the other then all your terms will be broken…not good. 

The point of this blog is not help you sync your environments with metadata.  That process is pretty well documented, and there are various ways to do this depending on your need:

  • Mount metadata database
  • Use PowerShell and Export-SPMetadataWebServicePartitionData/Import-SPMetadataWebServicePartitionData cmdlets (my preferred method for full syncs)
  • Tools, either codeplex or purchased:

The last tool provided a way to do what I needed, which was just to update some existing term sets with a few new terms in development that existed in production but keeping the same GUID.  You can even use it to update a GUID!  But the default language in my case was not english, and this tool threw errors.  So I turned to my trusty friend PowerShell.  I want to show you how to create a metadata term in the store using a GUID you specify so that it will match the term in production.  If you can though, I highly recommend you try the tool, it’s quite awesome and will do exactly that without any effort.  Let’s look at the different objects we can do with the metadata service via PowerShell.

PowerShell - Connecting to the Taxonomy Service

To create terms, groups, or term sets or do anything in the taxonomy instance you need to create a connection to it like the following:

#Connect to the Metadata Service
$taxSite = get-SPSite "<site URL>"
$taxonomySession = Get-SPTaxonomySession -site $taxSite
$termStore = $taxonomySession.TermStores["Managed Metadata Service"]


Replace site URL with any site collection URL or of Central Admin, and replace “Managed Metadata Service” with the name of the Metadata service application.
You can do a lot at this level, to see what you can run the following:

$termstore | get-member
There are multiple methods and properties that are useful, like creating groups and termstore administrators, and looking up terms and termsets:
sharepoint Create metadata term with powershell with guid
Once we have an instance to the Taxonomy Service, we can look up or create whatever we need. Depending on your need, you might only need to:
  • create a term in an existing term set
  • create a term set in an existing group
  • lookup an existing group

PowerShell - Creating Taxonomy Groups

The highest level you can work with in the term store is a Group.  If you are a term store administrator, you can use the UI:

sharepoint Create metadata term with powershell with guid

But we want to use PowerShell.  Here’s an example of creating a new taxonomy group, and connecting to an existing group:

#Create a group
$termGroup = $termstore.CreateGroup("New Group")

#Connect to existing term group
$termGroup = $termstore.groups["<group name>"];

Link to MSDN info for CreateGroup

When working term groups, you can also specify term group contributors and managers:

$termGroup.AddGroupManager(“contoso\tado”) $termGroup.AddContributor(“contoso\tado”) $termStore.CommitAll()
sharepoint Create metadata term with powershell with guid

PowerShell – Creating Term Sets

Going down the hierarchy, from groups now we look at term sets.  You can create it by just using a name, or a name and a GUID. 

#Create a term set
$termSet = $termGroup.CreateTermSet(“New Term Set”)

#Create a term set with GUID (change this GUID to yours)
$termSet = $termGroup.CreateTermSet(“New Term Set”,"9F6DAA27-49FA-4D34-A0F2-57106D416C36")

#Connect to existing term set
$termset = $TermGroup.Termsets["<term set name>"]

Link to MSDN info for CreateTermSet

PowerShell – Creating Terms

Now that we have a term set, we can get down to our goal… terms.  When creating terms, like term sets you can either just use the name or what we want to do, is use the GUID.  We will use the CreateTerm method from the TermSet, and we just specify the GUID along with the name and the language. 

#Create a term
$term = $termSet.CreateTerm("new term name")

#Create a term with GUID (change this GUID to yours)
$term = $termSet.CreateTerm("new term name",1033,"96625F47-4C74-48C8-B852-A91102ADEEBB")

Link to MSDN info for CreateTerm

Since creating a term with a GUID was our main objective, here’s a whole script for creating the term.  We connect to the existing group and term set, and then we create the term, then specify a description and a synonym.  1033 represents the language, and when we create the synonym, it takes a boolean if this is the default label, which we don’t so we set it to $false.

$taxonomySession = Get-SPTaxonomySession -Site "http://intranet"
$termStore = $taxonomySession.TermStores["Managed Metadata Service"];
$termgroup = $termstore.groups["New Group"];
$termset = $termgroup.TermSets["Products"];

$term = $termSet.CreateTerm("United States",1033,"CAD3FC14-7C32-44D2-BDD5-5F0B0FB788E9")
$term.SetDescription(“This is my description”, 1033)
$term.CreateLabel("USA”, 1033, $false)


Once complete, this is what we end up with:

sharepoint Create metadata term with powershell with guid

That’s all we need!  Again, I will recommend using the Metadata Navigation tool from codeplex, where you can do all this much easier.  But it’s a good exercise to do if you have no other option.

For more information on C5 Insight or this blog entry, please Contact Us.