SharePoint 2013 – On Premises – PowerShell script to import termsets on given site from .CSV file in specific folder.

Hi All,

In this article I’ll explain how to import multiple termsets using .CSV file from one specific folder. Means, all .CSV files (one .CSV file for one termset) are in one folder. PowerShell script will read all the .CSV files from given folder one by one and will import to the term store.
We will also check if group is already exists or not, if not then we will create new group.

I was trying to import multiple termsets from one import file (.CSV file) and after long time I realized that this is not possible. Through one import file (.CSV file) we can import only one termset. So if I have multiple termsets to import either I need to create multiple .CSV files or change one .CSV file multiple times.

In our project we need to check in the .CSV files for termsets to TFS so we decided to create the multiple .CSV files. And because of this, this article came.

Steps:

1. Check whether SharePoint snap in is loaded or not. If not then loading SharePoint snap in.

if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
       Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

2. Get the site / your site collection where termsets need to be imported

$site = Get-SPSite -Identity <“Your site collection URL”>

3. Get the taxonomy session and term store
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)

#Here we are assuming "Managed Metadata Service" termstore is available
$termstore = $session.TermStores["Managed Metadata Service"]

4. Taxonomy group – Checking if group is already exist or not. If not we will create a new group.
# Check for Taxonomy Group

$termGroup = <“Your group name”>
$group = $termstore.Groups[$termGroup]

       # Check if group exists or not
       if (!$group)
       {
              # creating the group
              $group = $termstore.CreateGroup($termGroup);
              $termstore.CommitAll()
}

5. Import manager : Get the import manager from Term Store

#getting Import manager for the Term Store
$manager = $termstore.GetImportmanager()

6. Importing each .CSV file :
#folder path
$termsFolderPath = “<Your folder path where all .csv files>”

#read all files
$files = ([System.IO.DirectoryInfo] (Get-Item $termsFolderPath)).GetFiles()
#looping through all files
ForEach($file in $files)
   {
       $reader = new-object System.IO.StreamReader($file.FullName)
       #output variables
              $alltermsadded = $false
              $errormessage = ""
       #import the termset file
       $manager.ImportTermSet($group, $reader, [ref] $alltermsadded, [ref] $errormessage)
}

Complete Script:

if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
       Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$site = Get-SPSite -Identity <“Your site collection URL”>

$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)

#Here we are assuming "Managed Metadata Service" termstore is available
$termstore = $session.TermStores["Managed Metadata Service"]

# Check for Taxonomy Group
$termGroup = <“Your group name”>
       $group = $termstore.Groups[$termGroup]

       # Check if group exists or not
       if (!$group)
       {
              # creating the group
              $group = $termstore.CreateGroup($termGroup);
              $termstore.CommitAll()
}

#getting Import manager for the Term Store
$manager = $termstore.GetImportmanager()
#folder path
$termsFolderPath = “<Your folder path where all .csv files>”

#read all files
$files = ([System.IO.DirectoryInfo] (Get-Item $termsFolderPath)).GetFiles()
#looping through all files
ForEach($file in $files)
       {
              $reader = new-object System.IO.StreamReader($file.FullName)
      
              #output variables
              $alltermsadded = $false
              $errormessage = ""
       #import the termset file
       $manager.ImportTermSet($group, $reader, [ref] $alltermsadded, [ref] $errormessage)
}

This site uses Akismet to reduce spam. Learn how your comment data is processed.