Skip to content

Instantly share code, notes, and snippets.

@Na0mir
Last active August 29, 2015 13:56
Show Gist options
  • Save Na0mir/9247410 to your computer and use it in GitHub Desktop.
Save Na0mir/9247410 to your computer and use it in GitHub Desktop.
Import termset from CSV file. CSV format is standard TermSet format.
# ----------------------------------------------
# Author: Microsoft
# Date: 27.02.2014
# Description: Import termset from CSV 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], 1033)
}
}
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
}
}
$session = Get-SPTaxonomySession -Site "http://localhost"
$store = $session.TermStores["Managed Metadata Service"]
$termSet = Import-Csv "C:\Users\Ronnie\Desktop\ImportTermSet.csv"
ImportTermSet $store "MyGroup" $termSet
$store.CommitAll()
Term Set Name Term Set Description LCID Available for Tagging Term Description Level 1 Term Level 2 Term Level 3 Term Level 4 Term Level 5 Term Level 6 Term Level 7 Term
Political Geography A sample term set, describing a simple political geography. True One of the seven main land masses (Europe, Asia, Africa, North America, South America, Australia, and Antarctica) Continent
True Entity defined by people, not visible to the naked eye Continent Political Entity
True Politically defined state with a geographic area governed by a central government Continent Political Entity Country
True Administrative division of a country Continent Political Entity Country Province or State
True Large sub-region usually containing many cities and towns Continent Political Entity Country Province or State County or Region
True Small village Continent Political Entity Country Province or State County or Region Hamlet
True Collection of homes and business, often incorporated Continent Political Entity Country Province or State County or Region Village
True A small city Continent Political Entity Country Province or State County or Region Town
True An incorporated town with a large population, usually governed by a mayor or council Continent Political Entity Country Province or State County or Region City
True A division of a city, often repesented in the city government Continent Political Entity Country Province or State County or Region City District
True A sub-section of a city Continent Political Entity Country Province or State County or Region City Borough
True Unofficial district or area of a city or town Continent Political Entity Country Province or State County or Region City Neighborhood
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment