1

I have create a global navigation using Term Store Management Tool in Root site collection for a web application. Now i want to copy this Navigation across site collection using powershell. Can you please help me on this. I have used the below power shell script for creating the Termsets. Basically what i want is a powershell script that will select the Term(CONTOSO) for all site collection at the Top navigation. Below is the screen hsot for the same

enter image description here

##Variables to be edited##############
$MMSService="Managed Metadata Service"
$CentralAdmin="http://SantoshVM:2013"
$CSVFILEPATH= "D:\SANTOSH\Navigation.csv"

##Variables that should not be edited
$groupname="Partners Navigation"

cls
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



function ImportTermSet([Microsoft.SharePoint.Taxonomy.TermStore]$store, [string]$groupName, [PSCustomObject]$termSet) {  
  function ImportTerm([Microsoft.SharePoint.Taxonomy.Group]$group, 
                      [Microsoft.SharePoint.Taxonomy.TermSet]$set, 
                      [Microsoft.SharePoint.Taxonomy.Term]$parent, 
                      [string[]]$path) {        
    if ($path.Length -eq 0) {
      return
    } elseif ($group -eq $null) {
      $group = $store.Groups | where { $_.Name -eq $path[0] }
      if ($group -eq $null) {
        $group = $store.CreateGroup($path[0])
      }
    } elseif ($set -eq $null) {
      $set = $group.TermSets | where { $_.Name -eq $path[0] }
      if ($set -eq $null) {
        $set = $group.CreateTermSet($path[0])
      }
    } else {
      $node = if ($parent -eq $null) { $set } else { $parent }
      $parent = $node.Terms | where { $_.Name -eq $path[0] }       
      if ($parent -eq $null) {
        $parent = $node.CreateTerm($path[0], 1033)
      } 
    }

    ImportTerm $group $set $parent $path[1..($path.Length)]
        Commit $store 2>&1 | out-null
    #$store.CommitAll()
  }

  function RemoveTermGroup([Microsoft.SharePoint.Taxonomy.TermStore]$store, [string]$groupName) {
    $group = $store.Groups | where { $_.Name -eq $groupName }
    if ($group -ne $null) {
      $group.TermSets | foreach { $_.Delete() }
      $group.Delete()
      $store.CommitAll()
    }
  }

  function Commit($store) {
    $store.CommitAll()
  }

  RemoveTermGroup $store $groupName
  $termSetName = $termSet[0]."Term Set Name"    
  $termSet | where { $_."Level 1 Term" -ne "" } | foreach {
    $path = @($groupName, $termSetName) + @(for ($i = 1; $i -le 7; $i++) { 
      $term = $_."Level $i Term"
        if ($term -eq "") {
          break
        } else {
          $term
        }
      }
    )

    ImportTerm -path $path
    $ErrorActionPreference = "Continue";
  }
  }


$CAsite =$CentralAdmin
Sleep 2
"Connecting to Term Store"
Write-host
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($CAsite)
$store = $session.TermStores[$MMSService]   
"Importing Term Set CSV File"
Write-Host
$termSet = Import-Csv $CSVFILEPATH
ImportTermSet $store $groupname $termSet
"All Term Sets have been imported"
Write-Host 

Your Answer

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

Browse other questions tagged or ask your own question.