Using PowerShell to upload hierarchical Term Store in SharePoint

The SharePoint Term Store has emerged as an important feature that is used extensively nowadays for various configuration options. For example, Term Store is used to store custom navigation items, custom left navigation items, and custom quick link items. It is even extensible, used to store constants for list columns, and act as a master data store for them. There are various other scenarios, where Term Store has come in handy.

Add Data to Term Store Using PowerShell
There is a challenge associated with large amounts of Term Store data population or manipulation. Out of the box, SharePoint provides an upload through xml or csv formats. But when using custom solutions, adding Term Store data using code/PowerShell becomes handier in terms of deployment.

In this article, we will see how to add data to term store using power shell. We will follow the similar approach as we used in adding lists/ libraries.

We will first create an xml and store the terms. Then our PowerShell script will read the xml, create the term store and add the terms.

We will consider a very simple term store to start with. Let us define the xml for the term store. Let us keep isAvailableForTagging attribute as part of the xml, so that the property can be set for each term:



  
    
      
       
    
  

Now let us write the script to create a term store using the above xml.
We will keep the script generic such that any properly structured xml can be imported to the store. The script code is self-explanatory.

# Script to create Term Store exclusively based on configuration files
# allows to create Term Hierarchy and configure the Tagging for individual items
# =================================================================================
function CreateTermStore([String]$ConfigFileName = "")
{

	Write-Host "Reading Configuration file: Process Starting ....." -ForegroundColor Green
	[string]$xmlpath = $ConfigFileName
	 
	$TermStoreData =  [xml](Get-Content ($xmlpath))
	if( $? -eq $false ) 
	{
		LogError "Could not read config file. Exiting ..."
		Stop-Transcript
		Stop-SPAssignment -Global
		Exit 0
	}

	Write-Host "Success: Reading Configuration file: Process Completed ..." -ForegroundColor Green
	 
	Write-Host "Accessing Site : Process Starting ...." -ForegroundColor Green
	
	$site = Get-SPWebApplication -includecentraladministration | where {$_.IsAdministrationWebApplication} | Select-Object -ExpandProperty Url

	 if($Error.Count -gt 0) 
	 {     
		Write-Host "Error: Accessing Site. Exiting Script... " -ForegroundColor Red
		Stop-Transcript
		Stop-SPAssignment -Global
		exit 1
	 } 
	 
	 Write-Host "Success: Accessing Site." -ForegroundColor Green
	 
	 Write-Host "Accessing Taxonomy Session : Process Starting ....." -ForegroundColor Green
	 $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site) 
	 
	 if($Error.Count -gt 0) 
	 {     
		Write-Host "Error: Accessing Taxonomy Session. Exiting Script... " -ForegroundColor Red
		Stop-Transcript
		Stop-SPAssignment -Global
		Exit 0
	 }
	 
	 Write-Host "Success: Accessing Taxonomy Session : Process Completed .....`n" -ForegroundColor Green

	 Write-Host "Creating Meta Store  : Process Starting ..." -ForegroundColor Green
	 
	 try
	 {
	 	$termstore = $session.TermStores[$TermStoreData.termstore.name]	
		$TermStoreData.termstore.group |
		 ForEach-Object {
		 	# create the group
		 	if ($termstore.Groups[$_.name.Replace("&", "&")] -eq $null)
		 	{
				$group = $termstore.CreateGroup($_.name);
		   		Write-Host -ForegroundColor Green "Added group : " $_.name "`n"		
		 	}
			else
			{
				$group = $termstore.Groups[$_.name.Replace("&", "&")]
			}
			
		 	$_.termset |
		 	ForEach-Object {
		 		## create the termset
				if($group.TermSets[$_.name.Replace("&", "&")] -eq $null)
				{
					Write-Host -ForegroundColor Green "CreateTermSet termset : " $_.name; "`n"		
		    		$termset = $group.CreateTermSet($_.name);
		  			if($_.isAvailableForTagging -eq "false")
	  				{
						$termset.IsAvailableForTagging = $false;	
	  				}
	  				else
	  				{	
						$termset.IsAvailableForTagging = $true;	
	  				}
						
					if($_.description -ne $null)
					{
						$termset.Description = $_.description;
					}
	  				
		    		Write-Host -ForegroundColor Green "Added termset : " $_.name; "`n"		
				}
				else
				{
					$termset = $group.TermSets[$_.name.Replace("&", "&")]
					if($_.Action -eq "Rename")
					{
						$termset.Name = $_.Name
					}
				}
				Write-Host -ForegroundColor Green "Set Terms"	
		    	SetTerms -termsetitem $termset -parentnode $_;
				if($_.Action -eq "Rename")
				{
					$termset.Name = $_.NewName
					Write-Host "Rename Term " $_.name " to " $_.NewName -ForegroundColor Green
				}

		 	}
		 }
		 
	 $termstore.CommitAll()
	}
	catch
	{
		$termstore.RollbackAll();
		Write-Host "Exception while creating Meta store. Exiting script..." -ForegroundColor Red
		Stop-Transcript
		Stop-SPAssignment -Global
		
		Exit -1
	}
}

Once the simple Term Store is created, it is recommended that you have more terms and groups and subgroups on the xml and play around with them.

August 11, 2015

Questions?

Email [email protected] with any questions you have pertaining to this course.

Follow Dynamics 101



About D101 Academy

Dynamics101.com is a Rand Group Knowledge Center intended to provide our clients, and the Microsoft community, with on-demand training.

Should you require personalized support for your business, Rand Group offers a variety of Business Advantage support plans to fit your business' needs.

View Rand Group's Microsoft support options.

top
New CPE Accredited Courses Now Available for Dynamics AX, GP, and NAVEARN CREDITS TODAY