I am using below PnP PowerShell command to create new Term.
New-PnPTerm
In my case, I would like to create new term with custom properties under the existing term.
I can't find a way to do this. Is this possible? if yes how can I do this?
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityPer my research, PnP PowerShell does not support creating a sub term currently.
We can achieve that using CSOM.
1.Download the latest SharePoint Online Management Shell.
2.Install the SharePoint Online Client Components SDK.
3.Connect to SharePoint Online as a global admin or SharePoint admin in Office 365. To learn how, see Getting started with SharePoint Online Management Shell.
4.Run the following commands:
#Specify tenant admin and URL
$User = "<admin>@tenant.onmicrosoft.com"
$Site = "https://tenant.sharepoint.com/sites/<site>"
$GroupName = "TestGroup"
$TermSetName = "TestSet"
$Term = "test1"
$SubTerm = "subTest1"
#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
#Bind to MMS
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context)
$Context.Load($MMS)
$Context.ExecuteQuery()
#Retrieve Term Stores
$TermStores = $MMS.TermStores
$Context.Load($TermStores)
$Context.ExecuteQuery()
#Bind to Term Store
$TermStore = $TermStores[0]
$Context.Load($TermStore)
$Context.ExecuteQuery()
#Bind to Group
$Group = $TermStore.Groups.GetByName($GroupName)
$Context.Load($Group)
$Context.ExecuteQuery()
#Bind to Term Set
$TermSet = $Group.TermSets.GetByName($TermSetName)
$Context.Load($TermSet)
$Context.ExecuteQuery()
#create term
$TermAdd = $TermSet.CreateTerm($Term,1033,[System.Guid]::NewGuid().toString())
$Context.Load($TermAdd)
$Context.ExecuteQuery()
#create subterm
$L2TermAdd = $TermAdd.CreateTerm($SubTerm,1033,[System.Guid]::NewGuid().toString())
$Context.Load($L2TermAdd)
$Context.ExecuteQuery()
More inforamtion:
I was able to get around this limitation with very little extra code When using the New-PnpTerm command, it will return a term object. Just call CreateTerm from that term object to create a child.
#First you must get the Context
$ctx = Get-PnPContext
#Create the new Parent Term
$newTerm = New-PnpTerm -Name "Pastel" -TermSet "Colours" -TermGroup "Company Colours" -Id "GUID"
#Use that parent to create the child term
$childTerm = $newTerm.CreateTerm("Blue", 1033, "New Term Guid")
#Have the context do the work
$ctx.Load($newTerm)
$ctx.ExecuteQuery()
Not technically all PnP powershell, but it allowed me to get around the limitation.