Office 365 – Create Managed Metadata Terms using CSOM with PowerShell


In a follow up to my previous post – "Office 365 – Output Managed Metadata Term Sets and Terms using CSOM with PowerShell"http://blogs.technet.com/b/fromthefield/archive/2014/03/03/office-365-output-managed-metadata-term-sets-and-terms-using-csom.aspx, I've written a sample PowerShell script that can be used to add Terms to a specific Term Set. The script uses a simple text file as input with each Term to be added listed on a separate line:

Five 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, $GroupName is the name of the group that contains the Term Set you wish to add Terms to, $TermSetName is the name of the Term Set you wish to add Terms to and $Terms is the location of the text file that contains the Terms to be added. 

#Please install the SharePoint client components SDK – http://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/site"
$GroupName = "Group"
$TermSetName = "Term Set"
$Terms = Get-Content "D:\terms.txt"

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

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

#Create Terms
Foreach ($Term in $Terms)
    {
    $TermAdd = $TermSet.CreateTerm($Term,1033,[System.Guid]::NewGuid().toString())
    $Context.Load($TermAdd)
    $Context.ExecuteQuery()
    }


Below is a screenshot of the result:

Brendan Griffin – @brendankarl

Comments (11)

  1. Anonymous says:

    Office 365 – Create Managed Metadata Terms using CSOM with PowerShell
    thank you

  2. Anonymous says:

    I’ve not tested this script with On-Prem so not sure if it will work. Do you have any further details?

  3. Vikas Bansal says:

    Its not working for SharePoint on premise(on my VM)
    Can you help.

  4. Anonymous says:

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

  5. kenny says:

    Hi Brendan,

    I tried to follow through your blog.
    I have installed SDK, include related dlls(client, clientruntime, taxonomy)

    I assign variable below
    $mms = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($ctxP)

    I receive error:
    Cannot convert argument "context", with value:
    "Microsoft.SharePoint.Client.ClientContext", for "GetTaxonomySession" to type
    "Microsoft.SharePoint.Client.ClientRuntimeContext": "Cannot convert the
    "Microsoft.SharePoint.Client.ClientContext" value of type
    "Microsoft.SharePoint.Client.ClientContext" to type
    "Microsoft.SharePoint.Client.ClientRuntimeContext"."

    dll version:
    client -> 15.0.4711.1000
    taxonomy -> 15.0.4420.1017

  6. @Kenny – is $ctxP a valid reference to a Site Collection?

  7. Shwetank says:

    Hi Brendan,

    Even am facing the same issue, which Kenny raised. Please Guide.

  8. anonymouscommenter says:

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

  9. @Shwetank – Please feel free to contact me via e-mail so that I can help further, I’m sure you can guess my address 🙂

  10. Richard says:

    @Brendan, curious to know of the solution to the issue Kenny and Shwetank ran into. I’m having the same problem. My $context is pointing to a site collection and I’ve tried it with every version of the client dlls I could get my hands on.

  11. Richard says:

    For me this did turn out to be a DLL versioning issue. After getting the latest client SDK and rebooting(reboot was important) it resolved the issue.