and it will ask the following Parameter:
Type: Import (or) Export
SiteCollectionURL: http://peakfinders.com/sites/peakfinder/
SubsiteUrl: /sites/peakfinder
Groupname: metadatagroupname
FileName:c:/peakfinder.xmlTo import and ecport .xml file of Managed metadata services termset
<#
.SYNOPSIS
Imports or exports a term set.
.DESCRIPTION
Imports or exports a term set. Faceted navigation and custom properties are ignored.
.EXAMPLE
.\TermSetImportExport -Mode Export -Url http://mysite.com -TermSetName "Site Navigation" -FileName mysite.nav.xml
#>
[CmdletBinding(PositionalBinding=$false)]
param(
[Parameter(Mandatory=$true,HelpMessage="Import|Export")]
[ValidateNotNullOrEmpty()]
[ValidateSet('Import','Export')]
[string] $Mode,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({if($_ -notlike "http://*" -and $_ -notlike "https://*" ) { throw "Invalid url." } else { $true }})]
[string] $SiteCollectionURL,
[Parameter(Mandatory=$true,HelpMessage="example: /sites/sitecollection/subsite (do no add a trailing slash)")]
[ValidateNotNullOrEmpty()]
[ValidateScript({if($_ -notlike "/*" ) { throw "Invalid path." } else { $true }})]
[string] $SubSitePath,
[Parameter(HelpMessage="Specify the name of the term group, else, the site collection's term group is used.")]
[ValidateNotNullOrEmpty()]
[string] $TermGroupName,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $TermSetName,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $FileName,
[Parameter(HelpMessage="Required if there are multiple term stores associated with the target web application.")]
[ValidateNotNullOrEmpty()]
[string] $TermStoreName
)
function LoadSnapIns
{
if (!(Get-PSSnapin "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue)) {
Write-Host "Loading Microsoft.SharePoint.Powershell snapin..."
Add-PSSnapin "Microsoft.SharePoint.Powershell"
Sleep 5
}
}
function GetNodeLevel([System.Xml.XmlNode]$xmlNode, [int]$level)
{
$parentNode = $xmlNode.ParentNode
if ($parentNode) {
if (!$level) { $level = 1 }
$level++
GetNodeLevel $parentNode $level
} else {
return $level
}
}
function GetTermStore
{
$termStore = $null
if (!$TermStoreName) {
if ($taxonomySession.TermStores.Count -eq 1) {
$termStore = $taxonomySession.TermStores[0]
} elseif ($taxonomySession.TermStores.Count -gt 1) {
throw "Multiple TermStores detected. TermStoreName must be specified."
} else {
throw "No TermStore exists."
}
} else {
$termStore = $taxonomySession.TermStores[$TermStoreName]
if (!$termStore) { throw "TermStore '$TermStoreName' does not exist." }
}
return $termStore
}
function GetTermGroup([bool]$createIfNotExist)
{
if (!$TermGroupName) {
$termGroup = $termStore.GetSiteCollectionGroup($web.Site, $createIfNotExist)
if (!$termGroup) { throw "No SiteCollectionGroup exists for this site collection. Please specify a value for TermGroupName." }
Write-Host "TermGroupName not specified. Using TermGroup '$($termGroup.Name)'" -ForegroundColor DarkYellow
} else {
$termGroup = $termStore.Groups[$TermGroupName]
if (!$termGroup) {
if ($createIfNotExist) {
Write-Host "Creating Term Group '$TermGroupName'..." -ForegroundColor DarkYellow
$termGroup = $termStore.CreateGroup($TermGroupName)
} else {
throw "TermGroup '$TermGroupName' does not exist."
}
}
}
return $termGroup
}
function GetTermSet([string] $id, [bool]$createIfNotExist)
{
$termSet = $termGroup.TermSets[$TermSetName]
if (!$termSet) {
if ($createIfNotExist) {
Write-Host "Creating Term Set '$TermSetName'..." -ForegroundColor DarkYellow
if ($id) {
$termSet = $termGroup.CreateTermSet($TermSetName, [Guid]::Parse($id))
} else {
$termSet = $termGroup.CreateTermSet($TermSetName)
}
} else {
throw "Term Set '$TermSetName' does not exist."
}
}
return $termSet
}
function ExportTerm([Microsoft.SharePoint.Publishing.Navigation.NavigationTerm]$term, [System.Xml.XmlNode]$parentNode)
{
$elem = $xmlDoc.CreateElement("Term")
$taxonomyTerm = $term.GetTaxonomyTerm()
$elem.SetAttribute("Id", $taxonomyTerm.Id)
#General Tab
$elem.SetAttribute("IsAvailableForTagging", $taxonomyTerm.IsAvailableForTagging)
$elem.SetAttribute("Labels", $taxonomyTerm.Labels.Value -join ";")
$description = $taxonomyTerm.GetDescription()
if (![string]::IsNullOrEmpty($description)) { $elem.SetAttribute("Description", $taxonomyTerm.GetDescription()) }
#Navigation Tab
$elem.SetAttribute("Title", $term.Title)
$elem.SetAttribute("LinkType", $term.LinkType)
$elem.SetAttribute("ExcludeFromGlobalNavigation", $term.ExcludeFromGlobalNavigation)
$elem.SetAttribute("ExcludeFromCurrentNavigation", $term.ExcludeFromCurrentNavigation)
if (![string]::IsNullOrEmpty($term.HoverText)) { $elem.SetAttribute("HoverText", $term.HoverText) }
if (![string]::IsNullOrEmpty($term.AssociatedFolderUrl)) { $elem.SetAttribute("AssociatedFolderUrl", $term.AssociatedFolderUrl) }
#Term-Driven Pages Tab
$elem.SetAttribute("FriendlyUrlSegment", $term.FriendlyUrlSegment)
if (![string]::IsNullOrEmpty($term.TargetUrlForChildTerms)) { $elem.SetAttribute("TargetUrlForChildTerms", $term.TargetUrlForChildTerms) }
if (![string]::IsNullOrEmpty($term.CatalogTargetUrlForChildTerms)) { $elem.SetAttribute("CatalogTargetUrlForChildTerms", $term.CatalogTargetUrlForChildTerms) }
if ($term.LinkType -eq [Microsoft.SharePoint.Publishing.Navigation.NavigationLinkType]::SimpleLink) {
$elem.SetAttribute("SimpleLinkUrl", $term.SimpleLinkUrl)
} else {
if (![string]::IsNullOrEmpty($term.CategoryImageUrl)) { $elem.SetAttribute("CategoryImageUrl", $term.CategoryImageUrl) }
if (![string]::IsNullOrEmpty($term.CatalogTargetUrl)) { $elem.SetAttribute("CatalogTargetUrl", $term.CatalogTargetUrl) }
if (![string]::IsNullOrEmpty($term.TargetUrl)) { $elem.SetAttribute("TargetUrl", $term.TargetUrl) }
}
$parentNode.AppendChild($elem) | out-null
foreach ($childTerm in $term.Terms) {
ExportTerm $childTerm $elem
}
}
function ExportTermSet
{
Write-Host ">> Exporting Term Set..." -ForegroundColor Yellow
$taxonomySession = Get-SPTaxonomySession -site $web.Url
$termStore = GetTermStore
$termGroup = GetTermGroup
$termSet = GetTermSet
$navTermSet = [Microsoft.SharePoint.Publishing.Navigation.NavigationTermSet]::GetAsResolvedByWeb($termSet, $web, $null)
$xmlDoc = new-object System.Xml.XmlDocument
#Create TermSet Node
$termSetNode = $xmlDoc.CreateElement("TermSet")
$termSetNode.SetAttribute("Name", $termSet.Name)
$termSetNode.SetAttribute("Id", $termSet.Id)
$termSetNode.SetAttribute("TermStoreId", $termStore.Id)
$termSetNode.SetAttribute("TermGroupId", $termGroup.Id)
$termSetNode.SetAttribute("IsAvailableForTagging", $termSet.IsAvailableForTagging)
$termSetNode.SetAttribute("IsOpenForTermCreation", $termSet.IsOpenForTermCreation)
$termSetNode.SetAttribute("IsNavigationTermSet", $navTermSet.IsNavigationTermSet)
if ($termSet.Stakeholders.Count -gt 0) { $termSetNode.SetAttribute("Stakeholders", $termSet.Stakeholders -join ";") }
if (![string]::IsNullOrEmpty($termSet.Description)) { $termSetNode.SetAttribute("Description", $termSet.Description) }
if (![string]::IsNullOrEmpty($termSet.Owner)) { $termSetNode.SetAttribute("Owner", $termSet.Owner) }
if (![string]::IsNullOrEmpty($termSet.Contact)) { $termSetNode.SetAttribute("Contact", $termSet.Contact) }
if ($navTermSet.IsNavigationTermSet) {
if (![string]::IsNullOrEmpty($navTermSet.TargetUrlForChildTerms)) { $termSetNode.SetAttribute("TargetUrlForChildTerms", $navTermSet.TargetUrlForChildTerms) }
if (![string]::IsNullOrEmpty($navTermSet.CatalogTargetUrlForChildTerms)) { $termSetNode.SetAttribute("CatalogTargetUrlForChildTerms", $navTermSet.CatalogTargetUrlForChildTerms) }
}
$xmlDoc.AppendChild($termSetNode) | out-null
#Create Term Nodes for TermSet
foreach ($term in $navTermSet.Terms)
{
ExportTerm $term $termSetNode
}
$xmlDoc.OuterXml | Out-File $FileName
Write-Host "<< Export Complete" -ForegroundColor Yellow
}
function ImportTerms([System.Xml.XmlNode]$xmlNode, $parentTerm)
{
foreach ($xmlTerm in $xmlNode.SelectNodes('Term'))
{
$termTitle = $xmlTerm.Attributes['Title'].Value
#format output to screen
$level = GetNodeLevel $xmlNode
$i = 0
while ($i -lt $level) {
Write-Host " " -NoNewline
$i++
}
Write-Host " creating term $termTitle..."
#Type-specific attributes
$navTermId = if ($xmlTerm.Attributes['Id']) { $xmlTerm.Attributes['Id'].Value } else { [System.Guid]::NewGuid() }
if ($xmlTerm.Attributes["LinkType"].Value -eq "FriendlyUrl") {
$navTerm = $parentTerm.CreateTerm($termTitle, [Microsoft.SharePoint.Publishing.Navigation.NavigationLinkType]::FriendlyUrl, $navTermId)
if ($xmlTerm.Attributes['CategoryImageUrl']) { $navTerm.CategoryImageUrl.Value = $xmlTerm.Attributes['CategoryImageUrl'].Value }
if ($xmlTerm.Attributes['CatalogTargetUrl']) { $navTerm.CatalogTargetUrl.Value = $xmlTerm.Attributes['CatalogTargetUrl'].Value }
if ($xmlTerm.Attributes['TargetUrl']) { $navTerm.TargetUrl.Value = $xmlTerm.Attributes['TargetUrl'].Value }
} else {
$navTerm = $parentTerm.CreateTerm($termTitle, [Microsoft.SharePoint.Publishing.Navigation.NavigationLinkType]::SimpleLink, $navTermId)
$urlPath = $xmlTerm.Attributes['SimpleLinkUrl'].Value -replace "/sites/peakfinders", $SubSitePath
$navTerm.SimpleLinkUrl = $urlPath
}
#Common attributes
$navTerm.FriendlyUrlSegment.Value = $xmlTerm.Attributes['FriendlyUrlSegment'].Value
$navTerm.ExcludeFromGlobalNavigation = [bool]::Parse($xmlTerm.Attributes['ExcludeFromGlobalNavigation'].Value)
$navTerm.ExcludeFromCurrentNavigation = [bool]::Parse($xmlTerm.Attributes['ExcludeFromCurrentNavigation'].Value)
if ($xmlTerm.Attributes['HoverText']) { $navTerm.HoverText = $xmlTerm.Attributes['HoverText'].Value }
if ($xmlTerm.Attributes['AssociatedFolderUrl']) { $navTerm.AssociatedFolderUrl.Value = $xmlTerm.Attributes['AssociatedFolderUrl'].Value }
if ($xmlTerm.Attributes['TargetUrlForChildTerms']) { $navTerm.TargetUrlForChildTerms.Value = $xmlTerm.Attributes['TargetUrlForChildTerms'].Value }
if ($xmlTerm.Attributes['CatalogTargetUrlForChildTerms']) { $navTerm.CatalogTargetUrlForChildTerms.Value = $xmlTerm.Attributes['CatalogTargetUrlForChildTerms'].Value }
$taxonomyTerm = $navTerm.GetTaxonomyTerm()
$taxonomyTerm.IsAvailableForTagging = [bool]::Parse($xmlTerm.Attributes['IsAvailableForTagging'].Value)
if ($xmlTerm.Attributes['Description']) { $taxonomyTerm.SetDescription($xmlTerm.Attributes['Description'].Value, 1033) }
$labels = $xmlTerm.Attributes['Labels'].Value.Split(';') | select -Skip 1
$labels | foreach-object { $taxonomyTerm.CreateLabel($_, 1033, $false) } | Out-Null
ImportTerms $xmlTerm $navTerm
}
}
function ImportTermSet
{
Write-Host ">> Importing Term Set..." -ForegroundColor Yellow
$taxonomySession = Get-SPTaxonomySession -site $web.Url
$termStore = GetTermStore
$termGroup = GetTermGroup $true
#fetch termset xml for import
$termSetXML = $($MyInvocation.PSScriptRoot+'\'+$FileName)
[xml]$xml = Get-Content $termSetXML
$termSetNode = $xml.DocumentElement
$termSetId = if ($termSetNode.Attributes['Id']) { $termSetNode.Attributes['Id'].Value } else { $null }
$termSet = GetTermSet $termSetId $true
$navTermSet = [Microsoft.SharePoint.Publishing.Navigation.NavigationTermSet]::GetAsResolvedByWeb($termSet, $web, $null)
#Import TermSet Properties
$termSet.IsAvailableForTagging = [bool]::Parse($termSetNode.Attributes["IsAvailableForTagging"].Value)
$termSet.IsOpenForTermCreation = [bool]::Parse($termSetNode.Attributes["IsOpenForTermCreation"].Value)
$navTermSet.IsNavigationTermSet = [bool]::Parse($termSetNode.Attributes["IsNavigationTermSet"].Value)
if ($termSetNode.Attributes['Stakeholders']) {
$stakeHolders = $termSetNode.Attributes['Stakeholders'].Value.Split(';')
$stakeHolders | foreach-object { $termSet.AddStakeholder($_) } | Out-Null
}
if ($termSetNode.Attributes['Description']) { $termSet.Description = $termSetNode.Attributes['Description'].Value }
if ($termSetNode.Attributes['Owner']) { $termSet.Owner = $termSetNode.Attributes['Owner'].Value }
if ($termSetNode.Attributes['Contact']) { $termSet.Contact = $termSetNode.Attributes['Contact'].Value }
if ($navTermSet.IsNavigationTermSet) {
if ($termSetNode.Attributes['TargetUrlForChildTerms']) { $navTermSet.TargetUrlForChildTerms.Value = $termSetNode.Attributes['TargetUrlForChildTerms'].Value }
if ($termSetNode.Attributes['CatalogTargetUrlForChildTerms']) { $navTermSet.CatalogTargetUrlForChildTerms.Value = $termSetNode.Attributes['CatalogTargetUrlForChildTerms'].Value }
}
ImportTerms $termSetNode $navTermSet
$termStore.CommitAll()
Write-Host "<< Import Complete" -ForegroundColor Yellow
}
function Main
{
[CmdletBinding()]
param()
Write-Host "Script started at $((Get-Date).ToShortTimeString())" -ForegroundColor Green
try {
LoadSnapIns
$web = Get-SPWeb $SiteCollectionURL -ErrorAction Stop
if ($Mode -eq "Import") {
ImportTermSet
} elseif ($Mode -eq "Export") {
ExportTermSet
}
} catch {
Write-Error $_
} finally {
if ($web) { $web.Dispose() }
}
Write-Host "Script finished at $((Get-Date).ToShortTimeString())" -ForegroundColor Green
}
Main
No comments:
Post a Comment