Import Or Export SharePoint Taxonomy Groups, Term Sets & Terms Using PnP PowerShell

Introduction

In this article, we will learn how we can import or export taxonomy groups, term sets and terms from/to SharePoint taxonomy term stores by several ways, using PnP PowerShell. The Client Side Object Model is used internally for these operations.

Prerequisite

You need to have PowerShell 3.0, available on a Windows machine. You need to install or import PnP PowerShell packages. You can download the installers or view more documentation from the official site. The installers are available here. Online version installer is preferred for On Premise or Office 365 operations. You can also install all the three installers for testing (SharePoint 2013, 2016, online).

The PnP PowerShell is supported by SharePoint 2013, SharePoint 2016 On Premises and Office 365 versions. The following operations are tested on SharePoint 2013 and Office 365 environments.

Connect To Site

Connect to the site, using the snippet given below. PnP PowerShell code, given below, helps in getting the current context of the site, using the Client Side Object Model (CSOM).

  1. $siteurl = "https://abc.sharepoint.com"  
  2. Connect-SPOnline -Url $siteurl  
  3. $ctx = Get-SPOContext   

Import

There are several ways to import the values from the taxonomy term stores.

Text File
 
The groups, term sets or the terms can be imported, using Import-SPOTaxonomy command. The required parameter for the operation is the path. Path indicates the text file, where the terms or the term sets are defined in the format, given below
 
Order is like this, TermGroup|TermSet|Term|SubTerm. 

Copy and save the context, given below, in text file as NewTermGroup.txt. Here, each value is separated by a separator(|). 
  1. TermGroup1|TermSet1|Term11  
  2. TermGroup1|TermSet1|Term12  
  3. TermGroup1|TermSet1|Term13  

The code snippet, given below, helps in creating a term store group along with the structure defined, given above.

  1. Import-SPOTaxonomy -Path "D:\Nakkeeran\PnP\NewTermGroup.txt"  
Arrays
 
The terms can be directly loaded as the arrays with the Import-SPOTaxonomy command. The required parameter comprises the terms.

The code snippet, given below, shows adding the term group, term sets and the terms on to term store, using the arrays.

  1. Import-SPOTaxonomy -Terms 'Locations|India|Karnataka|Bengaluru|JayaNagar','Locations|India|Karnataka|Bengaluru|HBRLayout','Locations|India|Karnataka|Mysuru'  
  2. Import-SPOTaxonomy -Terms 'Locations|India|TamilNadu|Chennai|Tambaram','Locations|India|TamilNadu|Chennai|Mylapore','Locations|India|TamilNadu|Vellore'  

XML

XML file can be imported to the term store, using the Import-SPOTermGroupFromXml command. XML file path is passed as a mandatory parameter.

The content, given below, shows the term set structure in XML format. Copy and save the content in XML file as ImportGroup.xml.

  1. <pnp:TermGroups xmlns:pnp="http://schemas.dev.office.com/PnP/2015/12/ProvisioningSchema">  
  2.   <pnp:TermGroup Name="TermGroup1" Description="">  
  3.     <pnp:TermSets>  
  4.       <pnp:TermSet Name="TermSet3" Description="">  
  5.         <pnp:Terms>  
  6.           <pnp:Term Name="Term31" />  
  7.           <pnp:Term Name="Term32" />  
  8.           <pnp:Term Name="Term33" />  
  9.         </pnp:Terms>  
  10.       </pnp:TermSet>  
  11.       <pnp:TermSet Name="TermSet4" Description="">  
  12.         <pnp:Terms>  
  13.           <pnp:Term Name="Term41" />  
  14.           <pnp:Term Name="Term42" />  
  15.         </pnp:Terms>  
  16.       </pnp:TermSet>  
  17.     </pnp:TermSets>  
  18.   </pnp:TermGroup>  
  19. </pnp:TermGroups>  

The snippet, given below, shows adding, using the XML file, given above.

  1. Import-SPOTermGroupFromXml -Path "D:\Nakkeeran\PnP\ImportGroup.xml"  

The following image shows the taxonomy store with the groups, term sets and terms updated.



Export

The groups, term sets and terms can be exported to the local file system in several ways.

Text

The term set structure can be exported, using Export-SPOTaxonomy command. The optional parameters for the operation are the path and term set Id. The path indicates the text file, where the terms or the term sets are required to be stored. Term set Id is used to import only the required term set.

Note: When the command is executed without any parameters, all the groups along with the term sets and terms will be listed on the interface.

The code snippet, given below, shows the operations. The term set Id can be identified from the term store collection.

  1. Export-SPOTaxonomy -Path "D:\Nakkeeran\PnP\Taxonomy.txt" -TermSetId "c4e29acb-5639-410d-9fa2-e3b251cb54a4"  

XML

The term groups along with the term sets and terms can be exported as XML, using Export-SPOTermGroupToXml command. The parameters which can be passed are the identity and out variable. The identity shows the group name. The out variable indicates the local file name along with the path. The code snippet, given below, shows the operation.
  1. Export-SPOTermGroupToXml -Identity "Locations" -Out "D:\Nakkeeran\PnP\TermGroup.xml"  

Summary

Thus, you have learned, how to import, export the groups, term sets and terms from/to SharePoint taxonomy term store programmatically by several ways, using PnP PowerShell commands. PnP PowerShell is supported by SharePoint 2013, SharePoint 2016 On Premises and Office 365 versions. The operations mentioned above, are tested on SharePoint 2013 and Office 365 environments.