Office 365 - Automating the Creation of Managed Metadata Groups, Term Sets and Terms using CSOM with PowerShell

I've previously written a Blog post on how to output MMS Term Sets and Terms using CSOM - https://blogs.technet.com/b/fromthefield/archive/2014/03/03/office-365-output-managed-metadata-term-sets-and-terms-using-csom.aspx. This next script example can be used to automate the creation of MMS Groups, Term Sets and Terms. The only reason that I wrote this script was for an excuse to improve my understanding of the CSOM API for MMS!

The script itself takes input in the form of an XML file, example below:

The script will create the MMS Groups, Term Sets and Terms contained within the XML file. I have attached an example XML file for reference.

Three variables need to be updated prior to running the script (highlighted), $User is the username of a tenant administrator, $Site is the URL of any Site within the tenant - this is simply used to bind to the Managed Metadata Service and $Import which is the location of the XML input file.

#Please install the SharePoint client components SDK - https://www.microsoft.com/en-us/download/details.aspx?id=35585 prior to running this script.

#Specify tenant admin and URL
$User = admin@tenant.onmicrosoft.com
$Site = "https://tenant.sharepoint.com/sites/CSOM"
[XML]$Import = Get-Content "D:\Example.xml"

#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\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\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)
$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()

Foreach ($GroupName in $Import.Groups.Group)
{
#Create Groups
$Group = $TermStore.CreateGroup($GroupName.Name,[System.Guid]::NewGuid().toString())
$Context.Load($Group)
$Context.ExecuteQuery()
    Foreach ($TermSetName in $GroupName.TermSets)
        {
        #Create Term Sets
        $TermSet = $Group.CreateTermSet($TermSetName.Name,[System.Guid]::NewGuid().toString(),1033)
        $Context.Load($TermSet)
        $Context.ExecuteQuery()
        Foreach ($TermName in $TermSetName.Terms.Term)
            {
            #Create Terms
            $TermAdd = $TermSet.CreateTerm($TermName,1033,[System.Guid]::NewGuid().toString())
            $Context.Load($TermAdd)
            $Context.ExecuteQuery()
            }
        }
}

Below is an example of how the Term Store will look after running the script with Example.XML file:

Brendan Griffin - @brendankarl

Example.xml