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.
Connect-PnPOnline -Url "https://domainName.sharepoint.com/sites/siteName"
$termSet = New-PnPTermSet -Name "Category" -TermGroup "Categories"
$term = New-PnPTerm -Name "Category1" -TermSet "Category" -TermGroup "Categories"
Explanation
- 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.
- The 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.
- Name and TermGroup are two parameters of this command. In the TermGroup parameter, we can give a name, unique ID, or term group object of the existing term group.
- The New PnPTerm command will add a term to our term set by the name we have passed to the -Name parameter.
- 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.
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 the taxonomy field as a site column or as a list column.
Add-PnPTaxonomyField -DisplayName "CategoriesField" -InternalName "CategoriesField" -TermSetPath "Categories|Category" -List "ListName"
Explanation
- The add-PnPTaxonomyField cmdlet will add a taxonomy field with the given parameters
- We need to pass the display name of our field to the parameter DisplayName
- InternalName will be the field's internal name.
- Now the most important part of the taxonomy is The termSetPath parameter. Path should be pipeline "|" separated and order should be "TermGroupName|TermSetName". Hence in our case, it is "Categories|Category"
- In the end, if we want to add a taxonomy field as a column in a list we have to pass one more parameter -List with the name of the particular list.
- If we don't pass -The list parameter with the 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.
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 a provisioning template of a site using this template we can create a 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!!