Create a TermSet And Taxonomy Field Using PnP Powershell

Introduction 
 
In this article, we will see how to create a TermSet and Terms and how to create Taxonomy site columns in SharePoint using PnP Powershell. Along with this, we will also see the differences between PnP Powershell and SharePoint Online Powershell commands.
 

Difference between PnP and SPO commands

 
First, we will see the differences between SharePoint Online Powershell commands and PnP Powershell commands. PnP commands run on the user's context whereas SPO commands run on the tenant admin account’s context. Hence, we will need tenant admin rights to run SPO commands, which is not always possible. PnP can be run on both SP Online and SP On-Premise, whereas SPO commands can be run over only SP Online.
 

Create TermSet and Terms

 
In the below code, we will see how we can create TermSet and Add Terms using PnP Commands:
  1. Connect-PnPOnline -Url "https://domainName.sharepoint.com/sites/siteName"
  2. $termSet = New-PnPTermSet -Name "Category" -TermGroup "Categories"
  3. $term = New-PnPTerm -Name "Category1" -TermSet "Category" -TermGroup "Categories"
Explanation
  1. The Connect-PnPOnline command will allow us to connect to the site where we want to create TermSet and Terms. -Url is a parameter passed to Connect-PnPOnline. You can replace "domainName" with your Domain and "siteName" with your site in the URL.
  2. New-PnPTermSet command will create a term set with the name "Category" to the existing term group "Categories". We can replace these names with the existing term group in our term store.
  3. -Name and -TermGroup are two parameters of this command. In the -TermGroup parameter, we can give name, unique id or term group object of the existing term group.
  4. The New-PnPTerm command will add term to our term set by the name we have passed to the -Name parameter.
  5. In the -TermSet and -TermGroup we can pass name, unique id or term set or term group object respectively.
We can see this in the below screenshot:
 
Create TermSet And Taxonomy Field Using PnP Powershell
 
Note
 
We should have site admin permissions to create the term set.
 

Create a Taxonomy Field

 
Using PnP Powershell commands we can also create Taxonomy fields. Isn’t it amazing!! Here we will see how to add taxonomy field as site column or as list column
  1. Add-PnPTaxonomyField -DisplayName "CategoriesField" -InternalName "CategoriesField" -TermSetPath "Categories|Category" -List "ListName"
Explanation
  1. Add-PnPTaxonomyField cmdlet will add taxonomy field with the given parameters
  2. We need to pass display name of our field to parameter -DisplayName
  3. -InternalName will be the field's internal name.
  4. Now the most important part of the taxonomy is -TermSetPath parameter. Path should be pipeline "|" separated and order should be "TermGroupName|TermSetName". Hence in our case, it is "Categories|Category"
  5. At the end, if we want to add taxonomy field as a column in a list we have to pass one more parameter -List with the name of the particular list.
  6. If we don&'t pass -List parameter with list name the field will be created as a site column.
We can see the selected TermSet, when we try to edit the field from the list or site columns where we have added this field using PnP PowerShell
 
Create TermSet And Taxonomy Field Using PnP Powershell
 

Other Benefits of PnP commands

 
Other than this, we can achieve many things using PnP commands e.g,
  • We can create Metadata Columns (site columns or list columns),
  • We can enable or disable site features,
  • We can get provisioning template of a site using this template we can create site,
  • We can add navigation nodes to the site,
  • We can add site designs and site scripts.
There are many more tasks that we can achieve using PnP commands, making our development easier.
 
References
 
Please refer to this link for more PnP cmdlets.
 
I hope this article is helpful to you.
 
Keep reading!!