Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 = "https://abc-admin.sharepoint.com/"
- $TermGroupName= "Site Collection - abc.sharepoint.com-sites-123site"
- $TermSetName="Due Diligence"
- $CSVFile ="C:Usersdm123DownloadsTermStore.csv"
- $TermHeaderInCSV ="Due Diligence"
- Try {
- #Get Credentials to connect
- $Username = "123@abc.com"
- $Password ="LE$b1yuio$!."
- $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $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
- }
Add Comment
Please, Sign In to add comment