This is a follow up to this post, where I described how you can create new website from template using Import-SPWeb. If you use managed metadata fields in the you will find out, that these fields cannot be used after importing of the website. The reason is, that the link to the managed metadata application is broken. This script will fix it:
$url = "http://yoursitecollection"
$termStoreName = "yourTermStoreName"
$termSetName = "nameOfTheLinkedTermSet"
$listName = "nameOfTheListWithManagedMetadataField"
$fieldName = "nameOfTheManagedMetadataField"
$web = Get-SPWeb $url
$session = Get-SPTaxonomySession -Site $url
$termStore = $session.TermStores[0]
$group = $termStore.Groups[$termStoreName]
$termSet = $group.TermSets[$termSetName]
$list = $web.Lists[$listName]
$field = $list.Fields[$fieldName]
$field.SspId = $termStore.Id
$field.TermSetId = $termSet.Id
$field.Update()
$list = $web.Lists[$listName]
$field = $list.Fields[$fieldName]
$taxonomyList = $web.Lists["TaxonomyHiddenList"]
$field.SchemaXml = $field.SchemaXml.Replace($field.LookupWebId.ToString(), $web.ID.ToString())
$field.SchemaXml = $field.SchemaXml.Replace($field.LookupList.ToString(), $taxonomyList.ID.ToString())
$field.Update()
$web.Dispose()
The script relinks the managed metadata field to the termstore and fixed the link to the webid and link to the taxonomy hidden list.
Tags: Powershell, SharePoint
Leave a Reply