FromTheField

Real world experiences of SharePoint CSS engineers from Microsoft UK

Office 365 - Create Managed Metadata Terms using CSOM with PowerShell

Office 365 - Create Managed Metadata Terms using CSOM with PowerShell

  • Comments 9
  • Likes

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
  • Office 365 - Create Managed Metadata Terms using CSOM with PowerShell
    thank you

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

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

  • 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

  • @Kenny - is $ctxP a valid reference to a Site Collection?

  • Hi Brendan,

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

  • @Shwetank - Please feel free to contact me via e-mail so that I can help further, I'm sure you can guess my address :)

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment