Thursday, November 28, 2013

Fix TaxonomyHiddenList guid after export/import of site collections in Sharepoint

In this post I will describe one more problem with using SPExport/SPImport API (see previous similar posts here, here and here). As you probably know TaxonomyHiddenList is special hidden list used by Sharepoint for using managed metadata on your site. It is located in the root site of site collection. One thing which is not so well known is that id of this list is also stored in the property bag of the site collection’s root web (SPWeb.Properties) in “TaxonomyHiddenList” key. The problem is that if you export existing site collection with SPExport API and then import it with SPImport with SPImportSettings.RetainObjectIdentity = false (which means that all objects in imported site will have new guids), list id is changed while value in property bag remains the same from source site collection. It causes many problems with managed metadata on exported site (e.g. it is not possible to save values in managed metadata fields).

In order to fix TaxonomyHiddenList value in the property bag I created the following PowerShell script:

   1:  function Fix-Taxonomy-Hidden-List($w)
   2:  {
   3:      $pv = $w.Properties["TaxonomyHiddenList"]
   4:      $l = $w.Lists["TaxonomyHiddenList"]
   5:      $id = $l.ID
   6:      Write-Host "    Property bag value: '$pv', list id: '$id'"
   7:      if ($pv.ToString().ToLower() -ne $id.ToString().ToLower())
   8:      {
   9:          Write-Host "    Property bag value differs from list id. It will be fixed" -foregroundcolor yellow
  10:          $w.Properties["TaxonomyHiddenList"] = $id
  11:          $w.Properties.Update()
  12:          Write-Host "    Property bag value was sucessfully fixed" -foregroundcolor green
  13:      }
  14:      else
  15:      {
  16:          Write-Host "    Property bag value equals to list id. Nothing should be fixed" -foregroundcolor green
  17:      }
  18:  }
  19:   
  20:  $wa = Get-SPWebApplication "http://example.com"
  21:  foreach ($s in $wa.Sites)
  22:  {
  23:      Write-Host "Checking $s.Url..."
  24:      Fix-Taxonomy-Hidden-List $s.RootWeb
  25:  }

It enumerates all site collections in specified web application (lines 21-25), checks value for “TaxonomyHiddenList” key in web property bag, compares it with TaxonomyHiddenList list guid and if they are different, it stores new value into property bag. You may run this script several times: after first time it will see that value in property bag equals the list id and won’t do anything. In order to use it on your environment change web application url on line 20 on your own. Hope it will help someone.

No comments:

Post a Comment