pnp / PnP-PowerShell

SharePoint PnP PowerShell CmdLets
https://pnp.github.io/powershell
Other
987 stars 662 forks source link

New-PnPTerm - Create Sub Term #1704

Open patadata opened 6 years ago

patadata commented 6 years ago

Reporting an Issue or Missing Feature

Missing feature

I can not understand how the ability to create sub terms has been left out!? Is it just me and all the organisations I have been working with as a consultant that has these demands?

My suggestion to how to implement this, syntax wise, would be:

New-PnPTerm -TermSet "Departments" -TermGroup "Corporate" -Name "Finance" -ParentTermId ab2af486-e097-4b4a-9444-527b251f1f8d

New-PnPTerm -Name "Finance" -ParentTermPath "Corporate|Departments|Economics"

I am running the latest version of both the 2016 and the online cmdlets and I would love to have this as a feature in these.

thinkb4code commented 6 years ago

You are not the only one @patadata, I am facing the same issue. And I am very much aligned to your suggestions (commands mentioned) as well for creating new Sub Terms.

Lucho1970 commented 6 years ago

Is it really not possible to create sub terms using the PnP? I have been pulling my hair out trying to find the correct format... then I found this post. I'll have to check the source for an answer, it has to be there.

patadata commented 6 years ago

@Lucho1970 nope, it does not work :(

Lucho1970 commented 6 years ago

I was able to get around this problem. Using the New-PnpTerm command, it will return a term object. Just call CreateTerm from that 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.

patadata commented 6 years ago

Good call!

I still don't like the mix in PnP-Powershell between actual PS and JSOM, it's a bit confusing, but as in this case, it could come in handy!

Thanx!

Lucho1970 commented 6 years ago

If I have some time (Yeah right!!) I'll try to create a pull request in the PnP library and add this functionality. It really isn't too hard.

holylander commented 5 years ago

I was able to get around this problem. Using the New-PnpTerm command, it will return a term object. Just call CreateTerm from that 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.

This approach didnt work form me, pnp-powershell complained that method was not available. As a workaround, I first retrieved the parent term, then created the new term, and then leverage the method "move", as described in the below example:

$ctx = Get-PnPContext
$parent_term = Get-PnPTerm -Identity "[desired_parent_termset" -TermSet "[termset_being_used]" -TermGroup "[term_group_being_used]" -Recursive -IncludeChildTerms
$new_term=New-PnPTerm -Name "[my_new_term]" -TermSet "[termset_being_used]" -TermGroup "[term_group_being_used]" -LCID "1033" -LocalCustomProperties @{_Sys_Nav_SimpleLinkUrl = "[a custom property example" }
$new_term.Move($parent_term)
$ctx.Load($new_term)
$ctx.ExecuteQuery()

easy ;-)

Markus-Hanisch commented 4 years ago

Hey, I just crossed this issue by accident because I'm also currently testing around with PnP Terms. In my opinion, there is a PowerShell cmdlet to create subterms: Import-PnPTaxonomy

holylander commented 4 years ago

Interesting, and how do you move an existing subterm move a certain parent term to another parent term? Would you mind providing a code example? thanks in advance

rsperre commented 4 years ago

Yet another way to create a sub term:

$parentTerm = Get-PnPTerm -TermGroup "GroupName" -TermSet "TermSet" -Identity "Parent Term Name" -IncludeChildTerms -Recursive
$parentTerm.CreateTerm("New child", 1033, (New-Guid))
Invoke-PnPQuery

@holylander - to move an existing term you could do this:

$termToMove = Get-PnPTerm -TermGroup "GroupName" -TermSet "TermSet" -Identity "Term Name" -IncludeChildTerms -Recursive
$newParent = Get-PnPTerm -TermGroup "GroupName" -TermSet "TermSet" -Identity "NewParentName" -IncludeChildTerms -Recursive
$termToMove.Move($newParent)
Invoke-PnPQuery