Copy Term Set from one Site Collection to Another in SharePoint 2010 through PowerShell


Ashok Kumar
SharePoint Consultant
Published On :   25 Dec 2012
Visit Count
Today :  5    Total :   11836
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


There was a need for me to move a sub site to a new site collection, I started it today thinking that it was a very simple task to do an import and export command.

Once after the site is restored there was a problem that a list in the site which had a meta data field did not work as expected. When any item in the list is opened for editing it showed a red line as if the metadata property does not exist.

I realized that the term sets are missing at the site collection level. Since there are only very few term sets it was easy for me to add them in to the term store. But even though term sets are added , the red line in the items still exist while trying to edit.

But the real problem is because each item which had the term set value internally had the ID property which had the same ID’s as that of the source site. Manually editing and saving the item once fixed the issue by updating the new GUID internally.

The following Powershell Script updated each Item programmatically.

 $TaxonomySiteUrl = "SITE URL"
 #Access the termstore
 $site = Get-SPSite $TaxonomySiteUrl
 $web = $site.OpenWeb()
 $list = $web.Lists["LISTNAME"]
 $query = New-Object Microsoft.SharePoint.SPQuery
 $listItems = $list.GetItems($query)
 
 foreach ($item in $listItems)
 {
     $targetField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$item.Fields["COLUMNNAME "]
     $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
     $termstore = $session.TermStores[$targetField.SspId]
     $termSet = $termstore.GetTermSet($targetField.TermSetId)
 
     $taxCollection = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection($targetField)
     #Write-Host "Value- ", $item["Doc_Type"]
     $termSet.GetAllTerms() | ForEach-Object {
     $taxonomyFieldValue = new-object Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue($targetField)
     $taxonomyFieldValue.TermGuid = $_.Id
     $taxonomyFieldValue.Label = $_.Name
         if($taxonomyFieldValue.Label -eq $item["COLUMNNAME"].Label)
         {
             Write-Host $taxonomyFieldValue
 
             $taxCollection.Add($taxonomyFieldValue)
             $targetField.SetFieldValue($item, $taxonomyFieldValue)
             $item.Update()
         }
     }
 }
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint