SharePoint 2013 - On Premises - Powershell Script To Import Termsets On Given Site From .csv File In Specific Folder

In this article, I’ll explain how to import multiple termsets using CSV files from one specific folder. This means all the CSV files (one CSV file for one termset) are in one folder. The PowerShell script will read all the CSV files from a given folder one by one and will import to the term store.

We will also check if the group already exists or not. If not, then we will create a new group.

Background

For the last 12 years, I have been working on SharePoint but today, I realized more practice/knowledge is required.  I was trying one thing which seems impossible. I was trying to import multiple termsets from one import file (CSV file) and after a 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 modify 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 multiple CSV files. And because of this, this article came into existence.

I am using Visual Studio 2015 and for PowerShell scripts, I have installed “PowerShell Tools for Visual Studio 2015”. PowerShell tools allow us editing and debugging of PowerShell script in Visual Studio.

Steps

  1. Check whether SharePoint snap in is loaded or not. If not, then load SharePoint snap in.
    1. if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))  
    2. {  
    3.     Add-PSSnapin Microsoft.SharePoint.PowerShell;  
    4. }  
  1. Get the site / your site collection where termsets need to be imported.
    1. $site = Get-SPSite -Identity <“Your site collection URL”>  
  1. Get the taxonomy session and term store.
    1. $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)  
    2.  
    3. #Here we are assuming "Managed Metadata Service" termstore is available  
    4. $termstore = $session.TermStores["Managed Metadata Service"
  1. Taxonomy group – Checking if the group already exists or not. If not, we will create a new group.
    1. # Check for Taxonomy Group  
    2.   
    3. $termGroup = <“Your group name”>  
    4.     $group = $termstore.Groups[$termGroup]  
    5.  
    6.     # Check if group exists or not  
    7.     if (!$group)  
    8.     {  
    9.         # creating the group  
    10.         $group = $termstore.CreateGroup($termGroup);  
    11.         $termstore.CommitAll()  
  1. Import manager - Get the import manager from Term Store
    1. #getting Import manager for the Term Store  
    2. $manager = $termstore.GetImportmanager()  
  1. Importing each CSV file.
    1. #folder path  
    2. $termsFolderPath = “<Your folder path where all .csv files>”  
    3.  
    4. #read all files  
    5. $files = ([System.IO.DirectoryInfo] (Get-Item $termsFolderPath)).GetFiles()  
    6. #looping through all files  
    7. ForEach($file in $files)  
    8.     {  
    9.         $reader = new-object System.IO.StreamReader($file.FullName)  
    10.      
    11.         #output variables  
    12.         $alltermsadded = $false  
    13.         $errormessage = ""  
    14.     #import the termset file  
    15.     $manager.ImportTermSet($group, $reader, [ref] $alltermsadded, [ref] $errormessage)  

Complete Script 

  1. if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))  
  2. {  
  3.     Add-PSSnapin Microsoft.SharePoint.PowerShell;  
  4. }  
  5.   
  6. $site = Get-SPSite -Identity <“Your site collection URL”>  
  7.   
  8. $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)  
  9.  
  10. #Here we are assuming "Managed Metadata Service" termstore is available  
  11. $termstore = $session.TermStores["Managed Metadata Service"]  
  12.  
  13. # Check for Taxonomy Group  
  14. $termGroup = <“Your group name”>  
  15.     $group = $termstore.Groups[$termGroup]  
  16.  
  17.     # Check if group exists or not  
  18.     if (!$group)  
  19.     {  
  20.         # creating the group  
  21.         $group = $termstore.CreateGroup($termGroup);  
  22.         $termstore.CommitAll()  
  23. }  
  24.  
  25. #getting Import manager for the Term Store  
  26. $manager = $termstore.GetImportmanager()  
  27. #folder path  
  28. $termsFolderPath = “<Your folder path where all .csv files>”  
  29.  
  30. #read all files  
  31. $files = ([System.IO.DirectoryInfo] (Get-Item $termsFolderPath)).GetFiles()  
  32. #looping through all files  
  33. ForEach($file in $files)  
  34.     {  
  35.         $reader = new-object System.IO.StreamReader($file.FullName)  
  36.      
  37.         #output variables  
  38.         $alltermsadded = $false  
  39.         $errormessage = ""  
  40.     #import the termset file  
  41.     $manager.ImportTermSet($group, $reader, [ref] $alltermsadded, [ref] $errormessage)  

Thanks!

As usual any comments/suggestions/feedback/questions are always welcome.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now