Skip to main content

PowerShell CSOM code to export term set from SharePoint online

 #Load SharePoint CSOM Assemblies

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"

   

#Variables for Processing

$AdminURL = "https://abc-admin.sharepoint.com"

$ReportOutput="C:\Temp\TermStoreData.csv"

 

Try {

    #Get Credentials to connect

    $Cred = Get-Credential

    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)


    Connect-SPOService -Url https://mmibi-admin.sharepoint.com #-Credential $Creds 

    Connect-MsolService #-Credential $Creds

 

    #Setup the context

    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL)

    $Ctx.Credentials = $Credentials

 

     #Array to Hold Result - PSObjects

    $ResultCollection = @()

 

    #Get the term store

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

    $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()

    $Ctx.Load($TaxonomySession)

    $Ctx.Load($TermStore)

    $Ctx.ExecuteQuery()

 

    #Get all term groups  

    $TermGroups = $TermStore.Groups

    $Ctx.Load($TermGroups)

    $Ctx.ExecuteQuery()

 

    #Iterate through each term group

    Foreach($Group in $TermGroups)

    {

        #Get all Term sets in the Term group

        $TermSets = $Group.TermSets

        $Ctx.Load($TermSets)

        $Ctx.ExecuteQuery()

 

        #Iterate through each termsset

        Foreach($TermSet in $TermSets)

        {

            #Get all Terms from the term set

            $Terms = $TermSet.Terms

            $Ctx.Load($Terms)

            $Ctx.ExecuteQuery()

 

            #Iterate through each term

            Foreach($Term in $Terms)

            {

                $TermData = new-object PSObject

                $TermData | Add-member -membertype NoteProperty -name "Group" -Value $Group.Name

                $TermData | Add-member -membertype NoteProperty -name "TermSet" -Value $Termset.Name

                $TermData | Add-member -membertype NoteProperty -name "Term" -Value $Term.Name  

                $ResultCollection += $TermData

            }

        }

    }

    #export term store sharepoint online powershell

    $ResultCollection | Export-csv $ReportOutput -notypeinformation

 

    Write-host "Term Store Data Successfully Exported!" -ForegroundColor Green  

}

Catch {

    write-host -f Red "Error Exporting Termstore Data!" $_.Exception.Message

}

Note: If need any help please reach out to me at kamal_pandey@outlook.com 

 I would be very happy to help. 

Comments

Popular posts from this blog

Code : Responsive Full Master Page : Left Navigation , Top Menu header and Footer

<%-- SPG: This HTML file has been associated with a SharePoint Master Page (.master file) carrying the same name.  While the files remain associated, you will not be allowed to edit the .master file, and any rename, move, or deletion operations will be reciprocated. To build the master page directly from this HTML file, simply edit the page as you normally would.  Use the Snippet Generator at https://goodyearcorp.sharepoint.com/sites/SPOTest2/_layouts/15/ComponentHome.aspx?Url=https%3A%2F%2Fgoodyearcorp%2Esharepoint%2Ecom%2Fsites%2FSPOTest2%2F%5Fcatalogs%2Fmasterpage%2FRadarOnlineBrasil%2Emaster to create and customize useful SharePoint entities, then copy and paste them as HTML snippets into your HTML code.   All updates to this file will automatically sync to the associated Master Page.  --%> <%@Master language="C#"%> <%@Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoin...

Business-critical processes with SharePoint and SQL

SharePoint 2010 provides the ability to connect to backend business systems, surface business data in SharePoint and make it accessible by, and useful to, employees across the organization. Almost every company of significant size uses ERP and CRM solutions to run core business processes. Over time, companies have developed detailed practices around using such systems to support vertical disciplines within the organization (e.g. product planning, financial performance management, supply-chain management, etc.),  yet challenges remain with regard to driving visibility and collaboration, based on business data, across different disciplines and teams. These challenges stem from the fact that only a fraction of employees are licensed and trained to use those backend systems, and from the high cost and complexity of integrating such systems across different functions and teams. With SharePoint and SQL, once the relevant business data is surfaced in an enterprise-wide...

PowerShell - CSOM code to get SharePoint Online Sites Permission Report

#sharepoint online powershell permissions report 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"          $AdminSiteURL="https://m365x067565-admin.sharepoint.com/"   #Connect to SharePoint Online Admin Write-host "Connecting to Admin Center..." -f Yellow Connect-SPOService -url $AdminSiteURL -Credential (Get-Credential) #Get each site collection and users $Sites = Get-SPOSite -Limit ALL Foreach($Site in $Sites) { $SiteURL=$Site.Url   } #To call a non-generic method Load Function Invoke-LoadMethod() {     param(             [Microsoft.SharePoint.Client.ClientObject]$Object = $(throw "Please provide a Client Object"),             [string]$PropertyName       ...