Monday, 11 August 2014

Export SharePoint Managed Metadata Terms using CSOM and PowerShell

SharePoint 2013 out of the box has provision for importing metadata terms from a CSV file but there is no provision to export the terms that have been created. Exporting the terms can be accomplished by using Get-SPTaxonomySession powershell cmdlet. Unfortunately this cmdlet  is not available for Office 365 online so cannot be used to export metadata terms from SharePoint Online.

An alternate approach is to connect to SharePoint Online Taxonomy Session through CSOM and export the terms to a file. Below script uses CSOM and a recursive function to loop through metadata terms in SharePoint Online and write them to a file

#Specify admin user and SharePoint site URL

$User = “User Name”

$Site = "SharePoint Online URL"

#Adding references to SharePoint client assemblies

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”

$Pwd = Read-Host -Prompt “Enter your password” -AsSecureString

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)

$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Pwd)

$Context.Credentials = $Credentials

$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context)



#Get Term Stores

$TermStores = $MMS.TermStores



$TermStore = $TermStores[0]



#Get Groups

$Groups = $TermStore.Groups



#Create the file and add headings

$OutputFile = "Output File Path"

$file = New-Object System.IO.StreamWriter($OutputFile)

$file.Writeline("Term Set Name,GUID");

Foreach ($Group in $Groups)




    $TermSets = $Group.TermSets



    Foreach ($TermSet in $TermSets)


        $file.Writeline($TermSet.Name + "," + $TermSet.Id);

        $Terms = $TermSet.Terms



        Foreach ($Term in $Terms)


                 $file.Writeline($Term.Name + "," + $Term.Id);







#Recursive function to get terms

function GetTerms([Microsoft.SharePoint.Client.Taxonomy.Term] $term)


 $SubTerms = $term.Terms;



            Foreach ($SubTerm in $SubTerms)


            $file.Writeline($SubTerm.Name + "," + $SubTerm.Id);




Note: Update the User, Site and OutputFile variables to values matching your environment.

1 comment:

  1. Thank you. I needed this to complete auto-provisioning of Content Organizer Rules with conditions