Office 365 – Moving and Merging Managed Metadata Terms using CSOM with PowerShell

I've previously posted about using CSOM to automate the addition and management of Managed Metadata in Office 365:

In this post I'll discuss how CSOM can be used in SharePoint Online to Move or Merge MMS Terms.

Moving a Term

In the example below I have a Group named Products, a Term Set named Entry Level and two categories of Products, Widget x and Widget y. Imagine that one of the products within this Term Set needs to move between the Widget lines of products, for example we may need to move Widget x1000 to the Widget y product line as part of a product re-structure within our company. MMS supports the ability to move a Term between Term Sets, which can be really useful. In my very contrived example I will use CSOM via PowerShell to move Widget x1000 to the Widget y product line - in a real world scenario it would be quicker to do this using the UI however if you have lots of changes to make (as one om my colleagues customers did) it makes sense to automate this!

The script below takes a source term $SourceTermName (Widget x1000) and moves this to a destination $DestTermSetName (Widget y). All you need to do is update the highlighted variables and execute! In the real world you would probably use an import file (CSV perhaps) that contains a list of terms to move and wrap this into a loop to automate the mind-numbing process of performing the move - this is purely a quick sample to get you started.

#Specify tenant admin and URL
$User = ""
$TenantURL = ""
$Site = ""
$GroupName = "Products"
$TermSetName = "Entry Level"
$SourceTermSetName = "Widget x"
$SourceTermName = "Widget x1000"
$DestTermSetName = "Widget y"

#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)

#Retrieve Term Stores
$TermStores = $MMS.TermStores

#Bind to Term Store
$TermStore = $TermStores[0]

#Retrieve Groups
$Groups = $TermStore.Groups

Foreach ($Group in $Groups)
Write-Host $Group.Name

$Group = $Groups | Where {$_.Name -eq $GroupName}

$TermSet = $Group.TermSets

$TermSet = $Group.TermSets | Where {$_.Name -eq $TermSetName}

$Terms = $TermSet.Terms

#Bind to Term Sets
$Source = $Terms | Where {$_.Name -eq $SourceTermSetName}

$Destination = $Terms | Where {$_.Name -eq $DestTermSetName}

$ChildTerms = $Source.Terms

$SourceTerm = $Source.Terms | Where {$_.Name -eq $SourceTermName}

$Move = $SourceTerm.Move($Destination)

Once the script has completed, it should look like this....

As you can see Widget x1000 is now within Widget y.

Merging a Term

Let's imagine that Widget x1000 is being discontinued and it's functionality will be incorporated into Widget y1000. In this scenario it may be desirable to merge the terms, which involves removing Widget x1000 as a separate entity and adding it as an additional label to Widget y1000.

The script below automates this operation, making the assumption that you have run the script above (they should be run in sequence as moving is a pre-requisite to merging). You simply need to update the highlighted variable with the name of the term you wish to merge to and Widget x1000 will be added as a label to Widget y1000.

$DestTermName = "Widget y1000"
$Destination = $Terms | Where {$_.Name -eq $DestTermSetName}

$ChildTerms = $Destination.Terms

$DestTerm = $Destination.Terms | Where {$_.Name -eq $DestTermName}

$SourceTerm = $Destination.Terms | Where {$_.Name -eq $SourceTermName}

$Merge = $SourceTerm.Merge($DestTerm)

All being well it should look something like this!

Brendan Griffin - @brendankarl

Comments (0)

Skip to main content