Office 365 - Create Managed Metadata Terms and Child Terms using CSOM with PowerShell


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

One of my colleagues was recently challenged by his customer to automate the creation of Terms along with child Terms in an existing Term Set using a CSV file as input, this is something that my previous scripts didn't cater for. An example input file that they provided can be found below:

In this specific example a Term exists for each country, beneath it is a Term for a City within the Country. L1T = Level 1 Term and L2T = Level 2 Term (Child).

I set about putting together a solution using PowerShell and SharePoint Online CSOM.

They already had an MMS Group created named Locations, the new Terms should be created within the relevant Locations Term Set (UK in this example). The following script takes the CSV input file above and creates the Terms and related Child Terms.

Simply update the highlighted variables and execute! The one thing you need to ensure is that the CSV file has the heading L1T and L2T (as above). $Site can be the URL of any Site Collection within your tenant, this is simply required to bind to the MMS Service Application.

$Terms = Import-CSV D:\Terms.csv
$Site = https://tenant.sharepoint.com
$GroupName = "Locations"
$TermSetName = "UK"

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"
$Username = Read-Host -Prompt "Enter your username"
$Password = Read-Host -Prompt "Enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)

#Bind to MMS
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$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()

$L1Terms = $Terms | Select L1T -Unique
Foreach ($Term in $L1Terms)
{
Write-Host "Creating L1 Term" $Term.L1T -ForegroundColor Green
$TermAdd = $TermSet.CreateTerm(($Term.L1T),1033,[System.Guid]::NewGuid().toString())
$Context.Load($TermAdd)
$Context.ExecuteQuery()
$L2Terms = $Terms | Where {$_.L1T -eq $Term.L1T}
Foreach ($L2Term in $L2Terms)
    {
    Write-Host "-L2 Term" $L2Term.L2T -ForegroundColor Yellow
    $L2TermAdd = $TermAdd.CreateTerm(($L2Term.L2T),1033,[System.Guid]::NewGuid().toString())
    $Context.Load($L2TermAdd)
    $Context.ExecuteQuery()
    }
}

Script execution in progress....

Once the script had been run this is how the Term Set looked:

Brendan Griffin - @brendankarl

Comments (2)

  1. anonymouscommenter says:

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

  2. SekThang says:

    Thanks Brenden for sharing this solution. This solution will help lot of office 365 Architects and developers for planning MMS deployment.

Skip to main content