O365 – Create Labels on Managed Metadata Terms using CSOM in PowerShell from a CSV file


The fourth post in a series of blog posts that walk through the process of performing configuration tasks against the Managed Metadata Term Store in O365 using CSOM in PowerShell. These posts will explain how to use a CSV file to provide the input values for the scripts however the logic within the scripts can be used to perform the same tasks without using a CSV file with minimal effort.

The example PowerShell script below can be used to take input values from a CSV file and create “Other Labels” on Managed Metadata Terms within a target Term Set in SharePoint Online. The script assumes that you will specify a path to a CSV file that contains the names of the terms and labels you wish to update and then loops through each line of the CSV file and creates the specified labels for each of the specified terms within the Term Set. I have attached a sample CSV file to this blog post.

It requires five variables to be updated – $User which is the tenant admin, $TenantURL which is the URL for the SharePoint Admin Center, $Site which is a valid URL of any SharePoint Site Collection that exists within the tenant, $GroupName which is the name of the Term Group you wish to connect to and $TermSetName which is the name of the Term Set that contains the terms on which you wish to create additional labels.

$User = "<Tenant Admin>"

$TenantURL = "<URL of SharePoint Admin Center>"

$Site = "<URL of Site Collection that exists in the tenant>"

$GroupName = "<Term Group Name that contains the target Term Set>"

$TermSetName = "<Target Term Set Name>"

 

#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

try

{

 

#Bind to the Managed Metadata Service within the target instance of SPO

$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 all of the Term Stores from the Managed Metadata Service

$TermStores = $MMS.TermStores

$Context.Load($TermStores)

$Context.ExecuteQuery()

 

#Bind to the Term Store you want to perform configurations against

$TermStore = $TermStores[0]

$Context.Load($TermStore)

$Context.ExecuteQuery()

 

#Bind to the Term Group you want to perform configurations against

$Group = $TermStore.Groups.GetByName($GroupName)

$Context.Load($Group)

$Context.ExecuteQuery()

 

#Bind to Term Set you want to perform configurations against

$TermSet = $Group.TermSets.GetByName($TermSetName)

$Context.Load($TermSet)

$Context.ExecuteQuery()

 

#Bind to Terms within the Term Set

$Terms = $TermSet.Terms

$Context.Load($Terms)

$Context.ExecuteQuery()

 

#Specify path to CSV input file that contains;

#The names of the Terms within the Term Set to configure,

#The values for the "Other Labels" property to apply for each Term in the Term Set

$path = "<Specify path to CSV file>"

 

#Import and load the CSV input file into the PowerShell Session

$csv = import-csv -path $path

 

#Process entries in the CSV

foreach($line in $csv){

 

#Get Terms by Name using the values supplied in the "TermNames" column of the CSV file.

$Term = $Terms.GetByName($line.TermNames)

$Context.Load($Term)

$Context.ExecuteQuery()

 

#Add "Other Labels" to Terms using the values supplied in the ("TermLabel1","TermLabel2" & "TermLabel3" columns of the CSV file.

$AddTermLabel = $term.CreateLabel($line.TermLabel1, 1033, $False)

$AddTermLabel = $term.CreateLabel($line.TermLabel2, 1033, $False)

$AddTermLabel = $term.CreateLabel($line.TermLabel3, 1033, $False)

$Context.Load($Terms)

$Context.ExecuteQuery()

}

}

 

catch

{

    write-host "Caught an exception:" -ForegroundColor Red

    write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red

    write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red

}

finally

{

    write-host ""

    write-host "Term Labels have been successfully updated on the target Terms" -ForegroundColor Green

 

 

SampleTermLabels.csv

Comments (2)

  1. Mary Whitney says:

    Thanks for the example! I changed the “$Terms = $TermSet.Terms” line to “$Terms = $TermSet.getAllTerms” so that this will add synonyms below the top level of the term set hierarchy. I also changed the final Context.Load parameter to $term instead of $Terms. (That’s the last one above the Catch section.)

    1. Mary Whitney says:

      Oops, I left out the parentheses after getAllTerms(). That should read “$Terms = $TermSet.getAllTerms()”

Skip to main content