SharePoint Pals
 | Sign In
Copy Term Set from one Site Collection to Another in SharePoint 2010 through PowerShell
by Ashok Kumar 25 Dec 2012
SharePoint Developer
Today  :   5     Total  :    10479

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
             $targetField.SetFieldValue($item, $taxonomyFieldValue)
blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 23, 22:21
How to Add/Remove User Custom Actions (in Site Actions Menu) Programmatically using CSOM PNP in SharePoint -

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation