Thursday, January 20, 2011

Creating Managed Metadata Termsets using PowerShell

Iterate through the XML file to dynamically create Managed Metadata Groups, Termsets and Terms using Powershell script.

Copy the XML mentioned below and save it as ManagedMetaDataTermSets.xml

<?xml version="1.0"?>
<termstore name="Managed Metadata Service">
  <group name="SharePoint Fix">
    <termset name="Region">
      <term name="North America">
        <term name="USA"></term>
        <term name="Canada"></term>
        <term name="Greenland"></term>
      </term>
      <term name="Technology">
        <term name="Technical Build and Delivery"></term>
        <term name="Technical Consultancy"></term>
        <term name="Technical Design"></term>
      </term>
      <term name="User Experience">
        <term name="Creative Design"></term>
        <term name="Information Architecture"></term>
      </term>
    </termset>
  </group>
</termstore>

The code mentioned below is RTM/Production Ready, you can find the original source code at Benn Robs site: http://sharepointtales.wordpress.com/2010/05/06/manipulating-the-terms-store-through-powershell/

Copy the code below and save it in a CreateTermSets.ps1 file: (replace the highlighted script block with your values)

Add-PsSnapin Microsoft.SharePoint.PowerShell

function SetTermsRecursive ([Microsoft.SharePoint.Taxonomy.TermSetItem] $termsetitem, $parentnode)
{
$parentnode.term
ForEach-Object {
## create the term
if($_ -ne $null)
{
$newterm = $termsetitem.CreateTerm($_.name, 1033)
Write-Host -ForegroundColor Cyan "Added term $_.name"
SetTermsRecursive $newterm $_
}
}
}
#Do not modify anything in the script from here onwards
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}

#Solutions to Deploy
$XMLName = "ManagedMetaDataTermSets.xml"
$XMLPath = Join-Path (Get-ScriptDirectory) $XMLName

echo "Extracting information from the $XMLPath"

#Site Collection URL - Give your site collection url in quotation marks
$TaxonomySiteUrl = "http://localhost"

#Access the TermStore data
[xml]$TermStoreData = Get-Content ($XMLPath)

$site = Get-SPSite $TaxonomySiteUrl
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termstore = $session.TermStores[$TermStoreData.termstore.name]
$TermStoreData.termstore.group

ForEach-Object {
## create the group
if ($termstore.Groups[$_.name] -eq $null)
{
$group = $termstore.CreateGroup($_.name);
Write-Host -ForegroundColor Cyan "Added group $_.name"
$_.termset

ForEach-Object {
## create the termset
$termset = $group.CreateTermSet($_.name)
Write-Host -ForegroundColor Cyan "Added termset $_.name"
SetTermsRecursive -termsetitem $termset -parentnode $_
}
}
}

$termstore.CommitAll()
Copy code below, save it as a .bat file to automatically run the powershell script
cd /d %~dp0
powershell -noexit -file ".\CreateTermSets.ps1" "%CD%"
pause

I have created another PowerShell script that reads through the XML file and delete the respective termsets and group:

Add-PsSnapin Microsoft.SharePoint.PowerShell

#Do not modify anything in the script from here onwards
function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

#Solutions to Deploy
$XMLName = "ManagedMetaDataTermSets.xml"
$XMLPath = Join-Path (Get-ScriptDirectory) $XMLName
echo "Extracting information from the $XMLPath"

#Site Collection URL - Give your site collection url in quotation marks
$TaxonomySiteUrl = "http://localhost"

#Access the TermStore data
[xml]$TermStoreData = Get-Content ($XMLPath)
$site = Get-SPSite $TaxonomySiteUrl
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termstore = $session.TermStores[$TermStoreData.termstore.name]

$TermStoreData.termstore.group |
 ForEach-Object {
  ## create the group
  if ($termstore.Groups[$_.name] -ne $null)
  {
    # $_.termset | ForEach-Object {
    #$termstore.Groups[$_.name].TermSet
    Write-Host -ForegroundColor Cyan "Deleted $termsetCollection"

 #Get the Term Store Group object
 $groupName = $termstore.Groups[$_.name]

 #Get Term Sets from the Group
 $groupName.TermSets | ForEach-Object {
   $_.Delete();
 }

# #Iterate through each
# foreach($termSet in $termstore.Groups[$_.name].TermSets)
# {
#  $termSet.Delete();
# }

 #Finally delete the Group
 $termstore.Groups[$_.name].Delete()
   }
  }
 $termstore.CommitAll()

I hope this helps.