SharePoint 2013 Taxonomy import using .csv and Setting site navigation to use taxonomy using PowerShell

Now requirement to import taxonomy from .csv file using PowerShell as well set taxonomy as site navigation with url.

1. Create .csv file


2. Create new script and paste below code.



 Param(  
  [string] $siteUrl = $(throw "Error: Parameter siteUrl is required"),  
  [boolean]$emptyfirst  
 )  
 ##Variables that should not be edited  
 $termsetName="My Navigation"  
 function CreateTerm( $parent, $name, $url )  
 {  
  Write-Host "Adding term $($parent.Name) -> $name"  
   $term = $parent.CreateTerm("$name", 1033)  
  $term.IsAvailableForTagging = $false  
  $term.SetLocalCustomProperty("_Sys_Nav_ExcludedProviders", '"CurrentNavigationTaxonomyProvider"')  
  $term.SetLocalCustomProperty("_Sys_Nav_SimpleLinkUrl", $url)  
  #$term.SetLocalCustomProperty("tIME", $url)  
  return $term  
 }  
 function GetTerm($termName, $parent)  
 {  
   $termName = [Microsoft.SharePoint.Taxonomy.TaxonomyItem]::NormalizeName($termName)  
   $term = $null  
  if( $termName -ne "" -and $parent -ne $null ){  
  if( $parent.Terms -ne $null ) {  
   $term = $parent.Terms | Where-Object {$_.Name -eq "$termName"}  
  }  
  if($term -eq $null ){  
   $term = CreateTerm -parent $parent -name "$termName" -url $_.URL  
  }  
  }  
  return $term;  
 }  
 function ImportTermSet([Microsoft.SharePoint.Taxonomy.TermSet]$set, [PSCustomObject]$terms) {   
  $terms | foreach {  
  $level1TermName = $_."Level 1 Term"  
  $level2TermName = $_."Level 2 Term"  
  $level3TermName = $_."Level 3 Term"  
   $level1Term = GetTerm -termName $level1TermName -parent $set  
  $level2Term = GetTerm -termName $level2TermName -parent $level1Term  
  $level3Term = GetTerm -termName $level3TermName -parent $level2Term   
  }  
  $ErrorActionPreference = "Continue";  
 }  
 Write-Host "Loading IIS module"  
 Import-Module WebAdministration  
 Write-Host "Loading SharePoint Commandlets"  
 Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue  
 Write-Host -ForegroundColor Green " Commandlets Loaded ... Loading Variables"  
 Write-Host "Connecting to Term Store"  
 Write-host  
 $termsetName="My Navigation"  
 $site = Get-SPSite $siteUrl  
 $web = $site.RootWeb  
 $session = [Microsoft.SharePoint.Publishing.Navigation.TaxonomyNavigation]::CreateTaxonomySessionForEdit($web)  
 $store = $session.TermStores[0]    
 $group = $session.TermStores.Groups | Where-Object {$_.SiteCollectionAccessIds -eq $site.ID }  
 $navigationSet = $group.TermSets | where { $_.Name -eq $termsetName }  
 if( $navigationSet -ne $null -and $emptyfirst) {  
  Write-Host -ForegroundColor Yellow "Removing existing termset"  
  $navigationSet.Delete()  
  $navigationSet = $null  
 }  
 if( $navigationSet -eq $null) {  
  Write-Host -ForegroundColor Yellow "Creating termset"  
  $navigationSet = $group.CreateTermSet($termsetName)  
 }  
 $navigationSet.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")  
 $navigationSet.SetCustomProperty("_Sys_Nav_AttachedWeb_SiteId", $site.ID.ToString())  
 $navigationSet.SetCustomProperty("_Sys_Nav_AttachedWeb_WebId", $site.RootWeb.ID.ToString())   
 $navigationSet.SetCustomProperty("_Sys_Nav_AttachedWeb_OriginalUrl", $site.RootWeb.Url )  
 #2013-02-05T12:52:07.5250653Z  
 $date = Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ"   
 $navigationSet.SetCustomProperty("_Sys_Nav_AttachedWeb_Timestamp", $date )  
 Write-Host "Importing Term Set CSV File"  
 $scriptpath = $MyInvocation.MyCommand.Path  
 $dir = Split-Path $scriptpath  
 $fileEntries = [IO.Directory]::GetFiles($dir);   
 foreach($fileName in $fileEntries)   
 {   
  $ext=[System.IO.Path]::GetExtension($fileName)  
  if($ext -eq ".csv")  
  {  
  Write-Host -ForegroundColor Green "Processing $fileName"  
  $CSVFILEPATH=$fileName;  
  $terms = Import-Csv -Delimiter ';' $fileName  
  ImportTermSet $navigationSet $terms  
  "All term sets have been imported"  
  Write-Host  
   }  
 }  
 $store.CommitAll()   
 Write-Host "Setting site nav to use taxonomy"  
 $settings = new-object Microsoft.SharePoint.Publishing.Navigation.WebNavigationSettings($web);  
 $settings.GlobalNavigation.Source = [Microsoft.SharePoint.Publishing.Navigation.StandardNavigationSource]::TaxonomyProvider  
 $settings.GlobalNavigation.TermStoreId = $store.Id  
 $settings.GlobalNavigation.TermSetId = $navigationSet.Id  
 $web.Update()  
 $settings.Update($session)  


Note :
1. Script file and .csv file should be in same folder.
2. This script supports import for multiple .csv files too. However it has to be in same folder.
3. Script will look for .csv in the current folder and will do import to Term store.
4.As well this script will set site navigation to use the taxonomy, It will set the navigation link with provided url to the term.




Comments

  1. Thank you very much Anuja, this post helped to solve my problem.

    ReplyDelete
  2. can you please explain the above code

    ReplyDelete
  3. Hi this is kind of what I am looking for but I want to generate a set of identical term Ids / Guids that for both development and prod. In my case some the terms as created in our UAT env will be used to tag data that I may need to port to production, hence I can't just do the a straight csv import.

    ReplyDelete
  4. I tried this but i dont see any option to pass the site url alnd the path of the csv file and also the file name. Can you please let me know if i want to update multiple termsets

    ReplyDelete
  5. How it will work for Sharepoint 365??

    ReplyDelete

Post a Comment

Popular posts from this blog

Search Query by ContentType name issue

SharePoint 2013 List CRUD operation using Angular and REST API

Search Result Webpart,Filter result If QueryString is present in URL