Create Metadata terms structure with CSV using Powershell

Tagging content is very important feature in SharePoint. This post will help you if you are planing to use tags and for initial configuration you don't want your client to manually create the 1000 of tags with a defined structure.

So first thing needed to built this is well defined structure of the terms in Excel file. So that you can easily create CSV file from Excel. While creating this excel file make sure that all the terms you enter this should comes under ONE TERM SET. For other terms set you will be needing another csv file.

You can take help from this sample csv file for creating site structure.

Next, is to open the PowerShell and write some code. Below is the code functions used for importing excel file.

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], 1044)
}
}


ImportTerm $group $set $parent $path[1..($path.Length)]
}


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()
}
}


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
}
}


In order to call these methods used below commands by changing parameters values:


$session = Get-SPTaxonomySession -Site "http://sharepoint-app:1000"
$store = $session.TermStores["Managed Metadata Service"]
$termSet = Import-Csv "F:\ImportTermSet_Regions.csv"
ImportTermSet $store "<group_name>" $termSet
$store.CommitAll()

That is all. Execute this and enjoy :)

Comments

  1. Hi Thanks for the Article, any idea how we could import TermDescription as well?

    ReplyDelete
  2. Hi Vishal,
    Thanks to you first for referring my blog.
    To answer your query, if you notice that the format of CSV file is like:

    Term Set Name,Term Set Description,LCID,Available for Tagging,Term Description,Level 1

    There you can set description for each term. Let me know if I didn't get your question.

    Regards
    Mohit Vashishtha

    ReplyDelete
  3. This is great! I was looking for something like this. But I have one Problem: What if I have also one (or maybe more) translations of my Terms. Let's say I want to expand the CSV-file so that there is next to every "Level x Term"-column a column called "Level x Term German". How do I need to Change your code that it will Import it correctly? I tried it with "CreateLabel" but I only get Error-Messages. Can you Help me with that?

    ReplyDelete
  4. Hi Christian, Thanks for trying my solution. If I understood your requirement correctly, you want to create term set of different language. You can do that by using the field "LCID" in the sample csv file. for eg. 1033 is for "Eng" and 1031 is for "GER". Find the list at: http://www.nedcomp.nl/support/origdocs/vbscript/extracted/html/vsmsclcid.aspx.
    Hope that helps you.

    ReplyDelete

Post a Comment

Popular posts from this blog

Hide Ribbon on SharePoint 2013 using CSS

Get Comment Count in SharePoint

Configure external site as content sources in sharepoint search