SharePoint Online: Get All Terms from a Term Set using Powershell

Requirement: PowerShell to Get All Terms from a Term Set in SharePoint Online
sharepoint online get all terms from a term set using powershell

SharePoint Online: Get Terms from a Term Set using PowerShell
#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" 

#Function to recursively get all child terms from a Term
Function Get-SPOTerm([Microsoft.SharePoint.Client.Taxonomy.Term]$Term)
{
    Write-host $Term.Name
    #Get All child terms
    $ChildTerms = $Term.Terms
    $Ctx.Load($ChildTerms)
    $Ctx.ExecuteQuery()

    #Process all child terms
    Foreach ($ChildTerm in $ChildTerms)
    {
        $Indent = $Indent +"`t"
        Write-host $Indent -NoNewline
        Get-SPOTerm($ChildTerm)
    }
    $Indent = "`t"
}

#Parameters
$AdminCenterURL = "https://crescenttech-admin.sharepoint.com"
$TermGroupName="Deals Pipeline"
$TermsetName="Dealstage"

#Get Credentials to connect
$Cred= Get-Credential
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminCenterURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Get the taxonomy session and termstore
$TaxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx)
$TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
$Ctx.Load($TaxonomySession)
$Ctx.Load($TermStore)
 
#Get the Term Group    
$TermGroup = $TermStore.Groups.GetByName($TermGroupName)
$Ctx.Load($TermGroup)
 
#Get the termset
$TermSet = $TermGroup.TermSets.GetByName($TermSetName)
$Ctx.Load($TermSet)
$Ctx.ExecuteQuery()

#Get all terms
$TermColl=$TermSet.Terms
$Ctx.Load($TermColl)
$Ctx.ExecuteQuery()

# Loop through all the terms
Foreach($Term in $TermColl)
{
    #Get the terms recursively
    Get-SPOTerm($Term)
}

PnP PowerShell to Get All Terms from a Term Set in SharePoint Online:
#Config Variables
$AdminCenterURL = "https://crescenttech-admin.sharepoint.com"

#Connect to PnP Online
Connect-PnPOnline -Url $AdminCenterURL -Credentials (Get-Credential)

#Get All Terms from a Term Set
Get-PnPTerm -TermSet "DealStage" -TermGroup "Deals Pipeline" 
result:
pnp powershell to get terms from term set

The Get-PnPTerm also supports -IncludeChildTerms -Recursive parameters!
SharePoint Online: Get All Terms from a Term Set using Powershell SharePoint Online: Get All Terms from a Term Set using Powershell Reviewed by Salaudeen Rajack on April 15, 2018 Rating: 5

1 comment:

  1. It is actually heplful. THank you for publishing such information.

    Can you also help me with my requirement?

    I want to fetch the details as in what terms have been assigned to what users.

    example:- we have used Location and created India, US, UK etc... which is now populating in front of Office Location under user profiles.

    Any help is appreciated

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.