1

I am facing a weird issue in regards to the PowerShell script I am using to import terms from CSV into a termset called Season. Source:http://www.sharepointdiary.com/2016/11/sharepoint-online-import-terms-to-termset-using-powershell.html

Below is the script:

#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://ignol-admin.sharepoint.com/"
$TermGroupName= "Site Collection - ignol.sharepoint.com-sites-testlpac"
$TermSetName="Season"
$CSVFile ="C:\Users\dm\Desktop\Sampleterm.csv"
$TermHeaderInCSV ="Season"

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 $CSVFile | ForEach-Object {

        #Get the Term Group
        $TermGroup=$TermStore.Groups.GetByName($TermGroupName)

        #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
        $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
}

Below is the error screenshot. I just don't understand why it says access denied, I am the site collection admin. enter image description here

Below is the CSV file screenshot.

enter image description here

Below is the term store screenshot.

enter image description here

Can someone please help me fix the error. Thanks in advance.

  • 1
    Check if account used in this script is term store admin. Sometimes even if the account is Global admin, the access may not work. It needs explicitly assigned permissions as a term store admin. – Lukas Nespor Jan 15 at 22:01
  • How can I check about Term store admin access assignment, can you help me with steps. Thanks for suggesting this. – mdevm Jan 15 at 22:02
  • 1
    How to set term store admin docs.microsoft.com/en-us/sharepoint/… – Lukas Nespor Jan 15 at 22:04
  • Thanks Lukar, it worked. Please post it as answer to so I can accept it. – mdevm Jan 16 at 1:08
1

Check if account used in the script is term store admin. Even if the account is Global admin, it needs explicitly assigned permissions as a term store admin for programmatical access to term store.

How to set term store admin
https://docs.microsoft.com/en-us/sharepoint/assign-roles-and-permissions-to-manage-term-sets

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.