Import Taxonomy csv file via Powershell

Importing a csv file containing your whole taxonomy structure in SharePoint 2010 is already described a lot.
Since i’m now working on a beta project in SharePoint 2010, I needed to create some deployment scripts.
How to import a metadata tree via PowerShell:
# Loading some variables
$weburl = "http://sharepointroot"
# Location of the tree-file
$StructureFileKnowHow = "C:\…\Binaries\Taxonomy\Structure_KnowHow.txt"
# loading the term store
$site = new-object Microsoft.SharePoint.SPSite($weburl)
$session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termstore = $session.TermStores["Managed Metadata Service"]
# Create a Taxonomy Group
# I first create a Term Group
$termGroup = "Knowledge Base"
$group = $termstore.CreateGroup($termGroup);
# Import Taxonomy "Know How"
# Create an instance of ImportManager
$Importer = $termstore.GetImportManager()
$varImported = ""
$varMessages = ""
$KnowHowReader = [System.IO.File]::OpenText($StructureFileKnowHow)
# Syntax: ImportTermSet(TermGroup, TextReader, out isImported, out errorMessage)
$Importer.ImportTermSet($group ,$KnowHowReader, ([REF] $varImported), ([REF] $varMessages))
I’m not a Powershell geek, i’ve used to write that kind of scripts but seven years ago, i suppose that you can write this script more compact (but less readable).
have fun!
This entry was posted in Sharepoint. Bookmark the permalink.

4 Responses to Import Taxonomy csv file via Powershell

  1. Anders says:

    Nice example! One addition i would recommend is to use the "new" syntax of establishing site context: $sites = Get-SPSite -url $url;Also remember disposing your sites using Start-SPAssignment and Stop-SPAssignment. More info:

  2. Steven says:

    thx Anders! disposing SPSite and SPWeb is common know via the OM, doing it also in Powershell can be "forgotten" 😉

  3. dudy1984 says:

    lot of thanks Steven!! it’s just I needed

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s