Hi,
There was requirement to import term sets csvs files all at a time into SharePoint Taxonomy.
Below is the script that has been written to pick up all term sets csv files from the folder where script file resides
// ImportTermSet_Batch.bat
Powershell.exe -Command Set-ExecutionPolicy "Bypass"
Powershell.exe -Command "& {D:\Naga\ImportTermSet\ ImportTermSet.ps1}"
Pause
// ImportTermSet.ps1
#-----Input parameters to the script
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint. Powershell'}
if ($snapin -eq $null)
{
Write-Host "Loading SharePoint Powershell Snap-in"
Add-PSSnapin "Microsoft.SharePoint. Powershell"
}
# Get complete file path (eg: E:\SP2010\xxxx.ps1)
$filepath = $MyInvocation.MyCommand. Definition
# Get current Directory file path (eg: E:\SP2010)
$directorypath = [System.IO.Path]:: GetDirectoryName($filepath)
# Get current Drive (eg: E:\)
$directory = Get-Item $directorypath | Split-Path -Parent
$InputFile = $directorypath+"\ ImportTermSetInputs.xml"
$fileEntries = [IO.Directory]::GetFiles($ directorypath);
$xmlinput = [xml] (get-content $InputFile)
$item = $xmlinput.TermSetInputs
## Start logging
$LogTime = Get-Date -Format yyyy-MM-dd_h-mm
$LogFile = $directorypath + "\Importing Term Set-$LogTime.rtf"
Start-Transcript -Path $LogFile -Force
#--------------------- Variables--------------------- --------------#
#$sitecollurl - to get Site Collection URL from xml
$sitecollurl = $item.sitecollectionurl
#$mmsserviceapp - to get Metadata Service Application Name from xml
$mmsserviceapp = $item.mmsname
#$termsetgroup - to get Term Set Group Name from xml
$termsetgroup = $item.termsetgroupname
#$csvfpath - to get csv file path from xml
##$csvfpath = $item.csvfilepath
#----------------------------- ------------------------------ -------#
#----------------------- Function to create terms----------------------#
try
{
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) {
Write-Host “Create $path[0]“
$set = $group.CreateTermSet($path[0])
Write-Host “Created $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)]
}
$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 $sitecollurl
$store = $session.TermStores[$ mmsserviceapp]
foreach($fileName in $fileEntries)
{
$ext=[System.IO.Path]:: GetExtension($fileName)
if($ext -eq ".csv")
{
Write-Host -ForegroundColor Green "Processing $fileName"
$CSVFILEPATH=$fileName;
$termSet = Import-Csv $CSVFILEPATH
ImportTermSet $store $termsetgroup $termSet
##$terms = Import-Csv -Delimiter ';' $fileName
##ImportTermSet $navigationSet $terms
Write-Host "All term sets have been imported"
}
}
$store.CommitAll()
Start-Sleep -Seconds 10;
}
catch
{
Write-Output $_
}
#----------------------------- ------------------------------ --#
$EndDate = Get-Date
Write-Host -ForegroundColor White "----------------------------- ------"
Write-Host -ForegroundColor White "| Term Set Imported |"
Write-Host -ForegroundColor White "| Started on: $StartDate |"
Write-Host -ForegroundColor White "| Completed: $EndDate |"
Write-Host -ForegroundColor White "----------------------------- ------"
Stop-Transcript
Invoke-Item $LogFile
// ImportTermSetInputs.xml
<?xml version="1.0" encoding="utf-8" ?>
<TermSetInputs>
<sitecollectionurl>http://abc. com/</sitecollectionurl>
<mmsname>Managed Metadata Service</mmsname>
<termsetgroupname>Group Name</termsetgroupname>
</TermSetInputs>
Finally we are getting the expected output as below
Note:
Before running the script make sure that the current account with which script is running up is added under Term Store administrator of Managed Metadata service
Hi Thanks for the article, any Idea what needs to be done to import TermDescritpion as well?
ReplyDeleteThis 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