Office 365 – Output Managed Metadata Term Sets and Terms using CSOM with PowerShell


This PowerShell script will connect to an O365 SharePoint tenant and output the following information to the console from the Managed Metadata Term Store:

  • Groups
  • Term Sets
  • Terms

As with my previous CSOM scripts, this is more a sample to get you started than something you would use in production. My term store has a relatively small amount of terms, I'm not sure how this script will behave with hundreds or thousands of terms!

Three variables need to be updated prior to running the script (highlighted), $User is the username of a tenant administrator, $TenantURL is the URL of the Tenant Admin Site and $Site is the URL of any Site within the tenant – this is simply used to bind to the Managed Metadata Service.

#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"
$TenantURL = "https://tenant.admin.sharepoint.com"
$Site = https://.sharepoint.com/sites/site

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

#Retrieve Groups
$Groups = $TermStore.Groups
$Context.Load($Groups)
$Context.ExecuteQuery()

#Retrieve TermSets in each group
Foreach ($Group in $Groups)
    {
    $Context.Load($Group)
    $Context.ExecuteQuery()
    Write-Host "Group Name:" $Group.Name -ForegroundColor Green
    $TermSets = $Group.TermSets
    $Context.Load($TermSets)
    $Context.ExecuteQuery()
    Foreach ($TermSet in $TermSets)
        {
        Write-Host "      Term Set Name:"$TermSet.Name -ForegroundColor Yellow
        Write-Host "        Terms:" -ForegroundColor DarkCyan
        $Terms = $TermSet.Terms
        $Context.Load($Terms)
        $Context.ExecuteQuery()
        Foreach ($Term in $Terms)
            {
            Write-Host "            " $Term.Name -ForegroundColor White
            }
        }
    }


Below is an example of the output from the script:



In my next post I plan to demonstrate how PowerShell can be used to create Term Sets and Terms using CSOM.

Brendan Griffin – @brendankarl

Comments (5)

  1. Anonymous says:

    Nice article

  2. Anonymous says:

    In a follow up to my previous post – "Office 365 – Output Managed Metadata Term Sets and Terms using

  3. Joe says:

    This script works well however it doesn’t appear to output any terms that are two or more levels deep in the term set structure. Would it be possible to modify the script to cater for these level 2+ terms?

    1. Thanks, yes it should be possible to do this you’d need to bind to each term and the check for child terms. If I get chance i may Blog about this in the future.

  4. Daniel Westerdale says:

    Hi

    This is very useful . I have simplified your code so that I use the support of the PnPCommandlets i.e. remove the need to detect the MMS . One thing that would be useful to understand if a term has sub terms and if so list them nn levels deep.