Wednesday, December 7, 2016

SharePoint Online: Delete Term Set using PowerShell

How to delete a Term Set in SharePoint Online?
To delete a term set from SharePoint online term store, follow these steps:

  • Login to your SharePoint Online admin center site. (E.g. https://yourdomain-admin.sharepoint.com)
  • Click the "Term store" link on the left navigation menu.
  • From the taxonomy tree view, Expand and select the term set you want to delete. Click on the little arrow in the Term set  >> Select "Delete Term Set" option. 
    SharePoint Online Delete Term Set using PowerShell
  • Confirm the prompt once to delete the term set from SharePoint Online term store.

PowerShell to Delete Term Set in 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://crescent-admin.sharepoint.com/"
$TermGroupName ="Regions"
$TermsetName="MENA"

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL)
    $Ctx.Credentials = $Credentials

    #Get the term store
    $TaxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx) 
    $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
    $Ctx.Load($TaxonomySession)
    $Ctx.Load($TermStore)
    $Ctx.ExecuteQuery()

    #Get the term group    
    $TermGroup=$TermStore.Groups.GetByName($TermGroupName)

    #Get the term set to delete
    $TermSet = $TermGroup.TermSets.GetByName($TermsetName)

    #Delete the term set
    $TermSet.DeleteObject()
    $Ctx.ExecuteQuery()

    Write-host "Term Set '$TermSetName' Deleted Successfully!" -ForegroundColor Green
}
Catch {
    write-host -f Red "Error Deleting Term Set!" $_.Exception.Message
} 

Delete All Term Sets from a Term Group in SharePoint Online:
How about deleting all term sets from a term group?
#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://crescent-admin.sharepoint.com/"
$TermGroupName ="Regions"

Try {
    #Get Credentials to connect
    $Cred = Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL)
    $Ctx.Credentials = $Credentials

    #Get the term store
    $TaxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx) 
    $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
    $Ctx.Load($TaxonomySession)
    $Ctx.Load($TermStore)
    $Ctx.ExecuteQuery()

    #Get the term group    
    $TermGroups = $TermStore.Groups
    $Ctx.Load($TermGroups)
    $Ctx.ExecuteQuery()
    $TermGroup = $TermGroups | Where-Object {$_.Name -eq $TermGroupName}
     
    If($TermGroup -ne $NULL)
    {
        #Delete all Term sets in the Term group
        $TermSets = $TermGroup.TermSets
        $Ctx.Load($TermSets)
        $Ctx.ExecuteQuery()

        #Delete all Term Sets from the Group
        $TermSets | Foreach-object {
            $_.DeleteObject()
            $Ctx.ExecuteQuery()
        }
     
        Write-host "All Term Sets Deleted Successfully from the Term Group '$TermGroupName'!" -ForegroundColor Green
    }
    else
    {
        Write-host "Term Group '$TermGroupName' Doesn't Exists!" -ForegroundColor Yellow
    } 
}
Catch {
    write-host -f Red "Error Deleting Term Sets!" $_.Exception.Message
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...