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
- Check whether SharePoint snap in is loaded or not. If not, then load SharePoint snap in.
- if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
- {
- Add-PSSnapin Microsoft.SharePoint.PowerShell;
- }
- Get the site / your site collection where termsets need to be imported.
- $site = Get-SPSite -Identity <“Your site collection URL”>
- 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"]
- Taxonomy group – Checking if the group already exists 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()
- }
- Import manager - Get the import manager from Term Store
- #getting Import manager for the Term Store
- $manager = $termstore.GetImportmanager()
- 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)
- }
Thanks!
As usual any comments/suggestions/feedback/questions are always welcome.