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.
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).
$siteurl = "https://abc.sharepoint.com"
Connect-SPOnline -Url $siteurl
$ctx = Get-SPOContext
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(|).
The code snippet, given below, helps in creating a term store group along with the structure defined, given above.
Import-SPOTaxonomy -Path "D:\Nakkeeran\PnP\NewTermGroup.txt"
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.
Import-SPOTaxonomy -Terms 'Locations|India|Karnataka|Bengaluru|JayaNagar','Locations|India|Karnataka|Bengaluru|HBRLayout','Locations|India|Karnataka|Mysuru'
Import-SPOTaxonomy -Terms 'Locations|India|TamilNadu|Chennai|Tambaram','Locations|India|TamilNadu|Chennai|Mylapore','Locations|India|TamilNadu|Vellore'
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.
<pnp:TermGroup Name="TermGroup1" Description="">
<pnp:TermSet Name="TermSet3" Description="">
<pnp:Term Name="Term31" />
<pnp:Term Name="Term32" />
<pnp:Term Name="Term33" />
<pnp:TermSet Name="TermSet4" Description="">
<pnp:Term Name="Term41" />
<pnp:Term Name="Term42" />
The snippet, given below, shows adding, using the XML file, given above.
Import-SPOTermGroupFromXml -Path "D:\Nakkeeran\PnP\ImportGroup.xml"
The following image shows the taxonomy store with the groups, term sets and terms updated.
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.
Export-SPOTaxonomy -Path "D:\Nakkeeran\PnP\Taxonomy.txt" -TermSetId "c4e29acb-5639-410d-9fa2-e3b251cb54a4"
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.
Export-SPOTermGroupToXml -Identity "Locations" -Out "D:\Nakkeeran\PnP\TermGroup.xml"
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.