Wednesday, December 7, 2016

SharePoint Online: Delete Term Group in Term Store using PowerShell

How to Delete a Group in SharePoint Online Term Store:

  • Login to your SharePoint 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, Select the term group which you want to delete. Click on the little arrow in the Term group Header >> Click on Delete Term Set option
    powershell to delete term group in sharepoint online
  • If the term group has any terms in it, you'll get an error message."The group has one or more term sets and can not be deleted. Move or delete all term sets from the group before deleting the Group". As the error message says, you got to delete all terms sets of the group in order to delete the group.
    delete term group in sharepoint online term store

PowerShell to Delete a Term Group 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"

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()

    #Check if the given group exists
    $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()

        $TermSets | Foreach-object {
            $_.DeleteObject()
            $Ctx.ExecuteQuery()
        }
        $TermStore.CommitAll()

        #Delete the Term Group
        $TermGroup.DeleteObject()
        $Ctx.ExecuteQuery()
     
        Write-host "Term Group '$TermGroupName' Deleted Successfully!" -ForegroundColor Green
    }
    else
    {
        Write-host "Term Group '$TermGroupName' Doesn't Exists!" -ForegroundColor Yellow
    }
}
Catch {
    write-host -f Red "Error Deleting Term Group!" $_.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...