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

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()
         }
     }
 }
blog comments powered by Disqus

SharePoint Pals

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

Training

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 articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

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