How to import group to Term Store Management using PowerShell Script in SharePoint 2103

Advertisement
   
      The following PowerShell script can be used to import group to term store management. Here we have to give Service name , site Url , Path and Name of term Store.

##Variables to be edited##############
$MMSService="servicename"
$CentralAdmin="http://localhost"


$CSVFILEPATH= "C:\Group.csv"

##Variables that should not be edited
$groupname="Name_Of_Term_Store"

cls
Write-Host "Loading IIS module"
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

The following image to show "service name"





Advertisements
SHARE

Unknown

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment