Export All SharePoint Online Term Group, Term Sets and Terms to CSV

param (
    [Parameter(Mandatory=$true)]
        [string]$RootSiteCollectionUrl
)

# Paths to SDK. Please verify location on your computer.
Add-Type -Path “c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type -Path “c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll”

$Credentials = Get-Credential -Message “Please enter SPO Admin User Name and Password”
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($RootSiteCollectionUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credentials.UserName, $Credentials.Password)
$ctx.ExecuteQuery() 

$session = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($ctx)
$ctx.Load($session)
$ctx.ExecuteQuery()

$termstore = $session.GetDefaultSiteCollectionTermStore();
$ctx.Load($termstore)
$ctx.ExecuteQuery()

  $groups=$termstore.Groups
  $ctx.Load($groups)

  $ctx.ExecuteQuery()

  $Termstring = “”
  $Header=”TermGroup,TermSet,Term`r`n”
  $Termstring = $Header
  foreach($group in $groups)
  {
    $ctx.Load($group)
    $ctx.Load($group.TermSets)
    $ctx.ExecuteQuery()
    Write-Output $group.Name
    $Termstring +=”$($group.Name)`r`n”
    foreach($termset in $group.TermSets)
    {
        Write-Output ”    $($termset.Name)”
        $Termstring +=”,$($termset.Name)`r`n”
        $ctx.Load($termset)
        $ctx.Load($termset.Terms)
        $ctx.ExecuteQuery()
        foreach($term in $termset.Terms)
        {
            Write-Output ”        $($term.Name)”
            $Termstring +=”,,$($term.Name)`r`n”
        }
    }
  }

Out-File -InputObject $Termstring C:\Scripts\Terms.csv -Encoding ascii -Force
C:\Scripts\Terms.csv

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s