Requirement: Import terms to a term set in SharePoint Online from a CSV file using PowerShell.

How to Import Terms to a Term Set in SharePoint Online?

Here is my CSV file with a list of Terms for a Term set called “Job Title” and we can use PowerShell to bulk create terms to a term set.

sharepoint online import term set

SharePoint Online Import Terms to Term Set using PowerShell: 

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Taxonomy.dll"
#Variables for Processing
$AdminURL = ""
$TermGroupName= "People"
$TermSetName="Job Title"
$CSVFile ="C:TempImportTerms.csv" 
$TermHeaderInCSV ="JobTitle"

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

    #Get Termstore data from CSV and iterate through each row
    Import-Csv $CSVFile | ForEach-Object {
        #Get the Term Group

        #Get the term set
        $TermSet = $TermGroup.TermSets.GetByName($TermSetName)

        #CSV File Header Row in Term to Add
        $TermName = $_.$($TermHeaderInCSV)
        #Check if the given term exists already
        $Terms = $TermSet.Terms
        $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())
            Write-host "New Term '$TermName' Added Successfully!" -ForegroundColor Green
            Write-host "Term '$TermName' Exists Already!" -ForegroundColor Yellow
Catch {
    write-host -f Red "Error Importing Term store Data!" $_.Exception.Message

and the result:

sharepoint online import terms

Source link