Monday, November 7, 2016

SharePoint Online: Bulk Add Terms, Term Sets and Term Groups using PowerShell

Requirement: Bulk Add Terms, Term Sets and Term Groups from a CSV File into SharePoint Online term store.

Here is the format and data to import:


PowerShell Script to Bulk Add Terms, Term Sets and Groups to SharePoint Online Term Store:
##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/"

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) 
    $TaxonomySession.UpdateCache()
    $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
    $Ctx.Load($TaxonomySession)
    $Ctx.Load($TermStore)
    $Ctx.ExecuteQuery()

    #Get Termstore data from CSV and iterate through each row
    Import-Csv "C:\Users\salaudeen\Desktop\TermStoreData.csv" | ForEach-Object {
        
        $TermGroupName = $_.TermGroup
        $TermSetName = $_.TermSet
        $TermName = $_.Term

        #**** Step 1: Create Term Group ****/
        #Check if the given group exists already
        $TermGroups = $TermStore.Groups
        $Ctx.Load($TermGroups)
        $Ctx.ExecuteQuery()
        $TermGroup = $TermGroups | Where-Object {$_.Name -eq $TermGroupName}
     
        If(-not $TermGroup)
        {
            #Create Term Group
            Write-host "Creating Term Group '$TermGroupName'" -ForegroundColor Cyan
            $TermGroup = $TermStore.CreateGroup($TermGroupName, [System.Guid]::NewGuid().toString())
            $Ctx.Load($TermGroup)
            $Ctx.ExecuteQuery()
            $TermGroup.TermStore.CommitAll()
            $TaxonomySession.UpdateCache()

            Write-host "Term Group '$TermGroupName' Created Successfully!" -ForegroundColor Green
        }
        else
        {
            Write-host "Term Group '$TermGroupName' Exists Already!" -ForegroundColor Yellow
        }


        #**** Step 2: Create Term Set ****#
        #Check if the given term set exists already
        $TermSets = $TermGroup.TermSets
        $Ctx.Load($TermSets)
        $Ctx.ExecuteQuery()
        $TermSet = $TermSets | Where-Object {$_.Name -eq $TermSetName}
     
        If(-not $TermSet)
        {
            #Create Term Set
            Write-host "Creating Term Set '$TermSetName'" -ForegroundColor Cyan
            $TermSet = $TermGroup.CreateTermSet($TermSetName,[System.Guid]::NewGuid().toString(),1033)
            $Ctx.Load($TermSet)
            $Ctx.ExecuteQuery()
            $TermSet.TermStore.CommitAll()
            $TaxonomySession.UpdateCache()
     
            Write-host "Term Set '$TermSetName' Created Successfully!" -ForegroundColor Green
        }
        else
        {
            Write-host "Term Set '$TermSetName' Exists Already!" -ForegroundColor Yellow
        }


        #*** Step 3: Create Term ***#
        #Check if the given term exists already
        $Terms = $TermSet.Terms
        $Ctx.Load($Terms)
        $Ctx.ExecuteQuery()
        $Term = $Terms | Where-Object {$_.Name -eq $TermName}
    
        If(-not $Term)
        {
            #Create Term Set
            Write-host "Creating Term '$TermName'" -ForegroundColor Cyan
            $Term = $TermSet.CreateTerm($TermName,1033,[System.Guid]::NewGuid().toString())
            $Ctx.Load($Term)
            $Ctx.ExecuteQuery()
            $Term.TermStore.CommitAll()
            $TaxonomySession.UpdateCache()
            Write-host "New Term '$TermName' Added Successfully!" -ForegroundColor Green
        }
        else
        {
            Write-host "Term '$TermName' Exists Already!" -ForegroundColor Yellow
        }
    }
 }
Catch {
    write-host -f Red "Error Importing Term store Data!" $_.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...