Saturday, October 18, 2014

Fix managed metadata fields after updating sandbox solution using client object model in Sharepoint

In one of my previous posts (see Reactivate Web-scoped features from PowerShell using client object model in Sharepoint Online) I wrote about problem which you will face with when will update sandbox solution: all web-scoped features on existing sites will be deactivated, so you will need to activate them back after wsp update. In this post I will write about one more issue related with update of sandbox solutions: after update binding of all managed metadata fields with appropriate term sets become broken. I.e. fields became greyed out and you can’t select any value for them using managed metadata picker.

In another post Provision managed metadata term sets and fields for Sharepoint Online using client object model I showed how to provision managed metadata fields and term sets and bind them with each other via client object model. And I wrote that it is important to assign IDs explicitly to the term sets because they will be used for binding. These ids will also help for fixing fields’ bindings after wsp update.

The main idea is that after each wsp update you need to call function Bind-Managed-Metadata-Field which was showed in the previous article again:

   1: function Bind-Managed-Metadata-Field($ctx, $termStoreId, $fieldId, $termSetId)
   2: {
   3:     $rootWeb = $ctx.Web
   4:     $fields = $rootWeb.Fields
   5:     $ctx.Load($fields)
   6:     $ctx.ExecuteQuery()
   7:  
   8:     try
   9:     {
  10:         $field = $fields.GetById($fieldId)
  11:     }
  12:     catch
  13:     {
  14:         Write-Host "Field" $fieldId "not found in site columns collection"
  15:             -foregroundcolor red
  16:         return
  17:     }
  18:  
  19:     $taxField = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").
  20:         MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).
  21:         Invoke($ctx, $field)
  22:     $taxField.SspId = $termStoreId
  23:     $taxField.TermSetId = $termSetId
  24:     $taxField.TargetTemplate = ""
  25:     $taxField.AnchorId = [System.Guid]::Empty
  26:     $taxField.UpdateAndPushChanges($true)
  27:     $ctx.ExecuteQuery()
  28:     Write-Host "Field" $fieldId "was successfully bound to termset" $termSetId
  29:         -foregroundcolor green
  30: }
  31:  
  32: function Bind-Managed-Metadata-Fields($ctx, $xmlFilePath)
  33: {
  34:     Write-Host "Binding managed metadata fields to term sets" -foregroundcolor green
  35:     [xml]$xmlContent = (Get-Content $xmlFilePath)
  36:     if (-not $xmlContent)
  37:     {
  38:         Write-Host "Xml was not loaded successfully. Fields won't be bound to term sets"
  39:             -foregroundcolor Red
  40:         return
  41:     }
  42:     $termStore = Get-TermStore $ctx
  43:     $groups = $termStore.Groups
  44:     $ctx.Load($groups)
  45:     $ctx.ExecuteQuery()
  46:     $group = $groups | Where-Object {$_.Name -eq $xmlContent.Group.Name}
  47:     if (-not $group)
  48:     {
  49:         Write-Host "Group" $xmlContent.Group.Name "not found"
  50:             -foregroundcolor Red
  51:         return
  52:     }
  53:  
  54:     Bind-Managed-Metadata-Field $ctx $termStore.Id "{field id 1}" "{term set id 1}"
  55:     Bind-Managed-Metadata-Field $ctx $termStore.Id "{field id 2}" "{term set id 2}"
  56:     ...
  57:     Bind-Managed-Metadata-Field $ctx $termStore.Id "{field id n}" "{term set id n}"
  58: }
  59:  
  60: $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
  61: $context.AuthenticationMode =
  62:     [Microsoft.SharePoint.Client.ClientAuthenticationMode]::Default
  63: $securePassword = ConvertTo-SecureString $password -AsPlainText -Force
  64: $credentials =
  65:     New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,
  66: $securePassword)
  67: $context.Credentials = $credentials
  68:  
  69: function Bind-Managed-Metadata-Fields $ctx "termsets.xml"

This example uses the same xml file structure as in previous article. After that your managed metadata fields will work again after update of sandbox solution.

No comments:

Post a Comment