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. An online version installer is preferred for On-Premise or Office 365 operations. You can also install all 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

Import

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

Text File

The groups, term sets, or terms can be imported, using the 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 a text file as NewTermGroup.txt. Here, each value is separated by a separator(|).

TermGroup1 | TermSet1 | Term11  
TermGroup1 | TermSet1 | Term12  
TermGroup1 | TermSet1 | Term13

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"

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 to the 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

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 an XML file as ImportGroup.xml.

<pnp:TermGroups xmlns:pnp="http://schemas.dev.office.com/PnP/2015/12/ProvisioningSchema">
  <pnp:TermGroup Name="TermGroup1" Description="">
    <pnp:TermSets>
      <pnp:TermSet Name="TermSet3" Description="">
        <pnp:Terms>
          <pnp:Term Name="Term31" />
          <pnp:Term Name="Term32" />
          <pnp:Term Name="Term33" />
        </pnp:Terms>
      </pnp:TermSet>
      <pnp:TermSet Name="TermSet4" Description="">
        <pnp:Terms>
          <pnp:Term Name="Term41" />
          <pnp:Term Name="Term42" />
        </pnp:Terms>
      </pnp:TermSet>
    </pnp:TermSets>
  </pnp:TermGroup>
</pnp:TermGroups>

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.

TermStore

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 the 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"

XML

The term groups along with the term sets and terms can be exported as XML, using Export-SPOTermGroupToXml command. The parameters that 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"

Summary

Thus, you have learned, how to import and export the groups, term sets, and terms from/to SharePoint taxonomy term store programmatically in 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.