Guest User

Untitled

a guest
Jan 14th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #Load SharePoint CSOM Assemblies
  2. Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
  3. Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
  4. Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Taxonomy.dll"
  5.  
  6. #Variables for Processing
  7.  
  8. $AdminURL = "https://abc-admin.sharepoint.com/"
  9. $TermGroupName= "Site Collection - abc.sharepoint.com-sites-123site"
  10. $TermSetName="Due Diligence"
  11. $CSVFile ="C:Usersdm123DownloadsTermStore.csv"
  12. $TermHeaderInCSV ="Due Diligence"
  13.  
  14. Try {
  15. #Get Credentials to connect
  16. $Username = "123@abc.com"
  17. $Password ="LE$b1yuio$!."
  18. $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $Password)
  19.  
  20. #Setup the context
  21. $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminURL)
  22. $Ctx.Credentials = $Credentials
  23.  
  24. #Get the term store
  25. $TaxonomySession=[Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx)
  26. $TaxonomySession.UpdateCache()
  27. $TermStore =$TaxonomySession.GetDefaultSiteCollectionTermStore()
  28. $Ctx.Load($TaxonomySession)
  29. $Ctx.Load($TermStore)
  30. $Ctx.ExecuteQuery()
  31.  
  32. #Get Termstore data from CSV and iterate through each row
  33. Import-Csv $CSVFile | ForEach-Object {
  34.  
  35. #Get the Term Group
  36. $TermGroup=$TermStore.Groups.GetByName($TermGroupName)
  37.  
  38. #Get the term set
  39. $TermSet = $TermGroup.TermSets.GetByName($TermSetName)
  40.  
  41. #CSV File Header Row in Term to Add
  42. $TermName = $_.$($TermHeaderInCSV)
  43.  
  44. #Check if the given term exists already
  45. $Terms = $TermSet.Terms
  46. $Ctx.Load($Terms)
  47. $Ctx.ExecuteQuery()
  48. $Term = $Terms | Where-Object {$_.Name -eq $TermName}
  49.  
  50. If(-not $Term)
  51. {
  52. #Create Term Set
  53. Write-host "Creating Term '$TermName'" -ForegroundColor Cyan
  54. $Term = $TermSet.CreateTerm($TermName,1033,[System.Guid]::NewGuid().toString())
  55. $Ctx.Load($Term)
  56. $Ctx.ExecuteQuery()
  57. $Term.TermStore.CommitAll()
  58. $TaxonomySession.UpdateCache()
  59. Write-host "New Term '$TermName' Added Successfully!" -ForegroundColor Green
  60. }
  61. else
  62. {
  63. Write-host "Term '$TermName' Exists Already!" -ForegroundColor Yellow
  64. }
  65. }
  66. }
  67. Catch {
  68. write-host -f Red "Error Importing Term store Data!" $_.Exception.Message
  69. }
Add Comment
Please, Sign In to add comment