2

My problem is I can't reuse a term under another term. But reuse term under a termset works fine. How can I reuse a term under another term. What I'm trying to do here just works fine with web interface. But when it comes to powershell, I'm stuck.

Code below works fine but it puts reused term in the root of the target termset. But the source term is under level 2, I mean it's under another term, not direcly under root.

$sourceterm = $mytermstore.GetTerm("66c67639-db8a-4101-8778-afbf10bef644");
$tset = $mytermstore.GetTermSet("186fcb82-4aea-4a79-b2dd-77b3895b533b")
$reusedTerm = $tset.ReuseTerm($sourceterm, $False)
$mytermstore.CommitAll()

I tried to use a term as target like below but I get error. Obviously term does not have ReuseTerm method.

$sourceterm = $mytermstore.GetTerm("66c67639-db8a-4101-8778-afbf10bef644");
$parentTerm = $mytermstore.GetTerm("186fcb82-4aea-4a79-b2dd-blah")
$reusedTerm = $parentTerm.ReuseTerm($sourceterm, $False)
$mytermstore.CommitAll()

I'm trying to reuse the existing term powershell2 under Courses under next termset Variations Navigation (tr)

enter image description here

  • Have you found answer for your above query? Thanks – user44734 Aug 2 '15 at 17:56
1

I am using PnP (PnP PowerShell overview) to get the term. Verify you are using the latest version. Check out this documentation to get the Client Taxonomy Term members. For the on-premise see here. Not sure the differences but always check these documentations dependent on your environment. (online or on-premise)

Method 1:

#SharePoint Online tenant
$orgName="https://contoso.sharepoint.com"
try {
    write-host "Logging-in using SPO"
    $credFMS = Connect-PnPOnline -Url $orgName -SPOManagementShell
    write-host "Logged-in"
} catch {
    write-host "Logging-in using WebLogin"
    $credFMS = Connect-PnPOnline -Url $orgName -UseWebLogin
    write-host "Logged-in"
}

#static values to test
$entityGuid="70bad89b-173c-48e4-b640-2873a3a76179"
$entityName="TermNameTest"
$entityType="Managed Account"

#Gets the term store based on the connection. Since we are connected to the root site collection, it will get the root site collection Term Store
$termStore=Get-PnPSiteCollectionTermStore
$sourceTermSetName = "TermSetSourceTestValue"
$targetTermSetName = "TermSetTargetTestValue"
$sourceTermGroupName = "TermGroupTestValue"
$targetTermGroupName = "TargetTermGroupName"

# Check if it exist first. "id" on the Where could be replaced with "Name" if you know the name of the term Where name -Name -eq $entityName
$sourceTerm = Get-PnPTerm -TermSet $sourceTermSetName -TermGroup $sourceTermGroupName | Where id -eq $entityGuid

if($sourceTerm.Count -gt 0) {
    #Found! - Reuse the term
    #Reuse term will place term under termset
    $targetParentTerm = Get-PnPTerm -Identity $entityType -TermSet $targetTermSetName -TermGroup $targetTermGroupName -Recursive
    $reuseTerm = $targetParentTerm.ReuseTerm($sourceTerm, $True)
    $termStore.CommitAll()
}

Method 2: Even though the above worked for me, alternatively you can do the following using CSOM with PnP

#*** Connection / ****#
#Locate where your module files are located
$PathModule="C:\Program Files (x86)\SharePointPnPPowerShellOnline\Modules\SharePointPnPPowerShellOnline"
Add-Type -Path "$PathModule\Microsoft.SharePoint.Client.dll"
Add-Type -Path "$PathModule\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "$PathModule\Microsoft.SharePoint.Client.Taxonomy.dll"

$urlRoot="https://Contoso.sharepoint.com/"

$connectRoot=$null
try {
    write-host "Logging-in to $urlRoot using WebLogin"
    #return connection to this variable using the -ReturnConnection parameter
    # '-UseWebLogin' is used for browser log-in. Typically for multi-factor authentication environments
    $connectRoot = Connect-PnPOnline -Url $urlRoot -ReturnConnection -ErrorAction Stop -UseWebLogin
    write-host "Logged-in"
} catch { 
    write-host "Unable to login"
    #Exit script if unable to connect
    return;
}
#Get the current context
$Context = Get-PnPContext

$sourceGroupName="SourceGroupNameSample"
$targetGroupName="TargetGroupNameSample"
$sourceTermSetName="SourceTermSetNameSample"
$targetTermSetName="TargetTermSetNameSample"
#Static
$sourceTermEntityName="SourceTermNameSample"
$targetCategoryTerm="SourceParentTermNameSample"
$targetEntityTypeTerm="SourceChildTermNameSample"

#Taxonomy Session
$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context)
$Context.Load($MMS)
$Context.ExecuteQuery()
#Retrieve Term Stores
$TermStores = $MMS.TermStores
$Context.Load($TermStores)
$Context.ExecuteQuery()
#Term Store
$TermStore = $TermStores[0]
$Context.Load($TermStore)
$Context.ExecuteQuery()

#Source Group
$sourceGroup = $TermStore.Groups.GetByName($sourceGroupName)
$Context.Load($sourceGroup)
$Context.ExecuteQuery()
#Target Group
$targetGroup = $TermStore.Groups.GetByName($targetGroupName)
$Context.Load($targetGroup)
$Context.ExecuteQuery()
#Source TermSet
$sourceTermSet = $sourceGroup.TermSets.GetByName($sourceTermSetName)
$Context.Load($sourceTermSet)
$Context.ExecuteQuery()
#Source Term
$sourceTerm = $sourceTermSet.Terms.GetByName($sourceTermEntityName)
$Context.Load($sourceTerm)
$Context.ExecuteQuery()

#Create a term
$TermAdd = $sourceTermSet.CreateTerm("NewTerm436",1033,[System.Guid]::NewGuid().toString())
$Context.Load($TermAdd)
$Context.ExecuteQuery()

#Get the Target Term Set
$targetTermSet = $targetGroup.TermSets.GetByName($targetTermSetName)
$Context.Load($targetTermSet)
$Context.ExecuteQuery()

#Get the Parent Term
$targetCategoryTerm = $targetTermSet.Terms.GetByName($targetCategoryTerm)
$Context.Load($targetCategoryTerm)
$Context.ExecuteQuery()

#Get the Child Term
$targetTypeTerm = $targetCategoryTerm.Terms.GetByName($targetEntityTypeTerm)
$Context.Load($targetTypeTerm)
$Context.ExecuteQuery()

#Reuse Term to the Target Child Term
$targetTypeTerm.ReuseTerm($sourceTerm, $True)
$Context.Load($targetTypeTerm)
$Context.ExecuteQuery()

# Create Single Label
$sourceTerm.CreateLabel("LabelTest214",1033,$False)

#Create multiple Labels
$sourceTerm.CreateLabel("Test236",1033,$False)
$sourceTerm.CreateLabel("Test237",1033,$False)
#$Context.ExecuteQuery() # ExecuteQuery could be use here but since its called in the next lines, no need to call it now

#Get the labels collection
$sourceTermLabels = $sourceTerm.Labels
$Context.Load($sourceTermLabels)
$Context.ExecuteQuery()

for ($i = 0; $i -lt $sourceTermLabels.Count; $i++)
{
    write-host "Label Name: "$sourceTermLabels[$i].Value " / Is Default: " $sourceTermLabels[$i].IsDefaultForLanguage
}

Write-Host "End"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.