1: param( 
       2:     [string]$siteColl,
       3:     [int]$lcid
       4: )
       5:  
       6: function Fix-Content-By-Search-Web-Part($wp, $webPartManager)
       7: {
       8:     Write-Host "Change fallback language for web part" $wp.Title "to $lcid"
       9: -foregroundcolor green
      10:     if ($wp.DataProviderJSON.Contains(",`"FallbackLanguage`":-1,"))
      11:     {
      12:         $wp.DataProviderJSON =
      13: $wp.DataProviderJSON.Replace(",`"FallbackLanguage`":-1,", ",`"FallbackLanguage`":$lcid,")
      14:         $webPartManager.SaveChanges($wp)
      15:         Write-Host "Fallback language was successfully changed" -foregroundcolor green
      16:     }
      17:     else
      18:     {
      19:         Write-Host "Fallback language not found in DataProviderJSON. Web part will be skipped."
      20: -foregroundcolor yellow
      21:     }
      22: }
      23:  
      24: function Fix-Content-By-Search-Web-Parts-On-Page($item)
      25: {
      26:     $file = $item.File
      27:     Write-Host "Check web parts on page" $file.Url -foregroundcolor green
      28:  
      29:     $webPartManager = $file.Web.GetLimitedWebPartManager($file.Url,
      30: [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
      31:     $contains = $false
      32:     foreach ($wp in $webPartManager.WebParts)
      33:     {
      34:         if ($wp.GetType().FullName -eq
      35: "Microsoft.Office.Server.Search.WebControls.ContentBySearchWebPart" -and
      36: $wp.DataProviderJSON.Contains(",`"FallbackLanguage`":-1,"))
      37:         {
      38:             $contains = $true
      39:             break
      40:         }
      41:     }
      42:  
      43:     if (-not $contains)
      44:     {
      45:         Write-Host "Page" $file.Url "doesn't contain content by search web parts which should be fixed"
      46: -foregroundcolor green
      47:         return
      48:     }
      49:  
      50:     $shouldBePublished = $false
      51:     if ($file.Level -eq [Microsoft.SharePoint.SPFileLevel]::Published)
      52:     {
      53:         $shouldBePublished = $true
      54:     }
      55:  
      56:     if ($file.CheckOutType -ne [Microsoft.SharePoint.SPFile+SPCheckOutType]::None)
      57:     {
      58:        Write-Host "Undo checkout for page" $file.Url -foregroundcolor yellow
      59:        $file.UndoCheckOut()
      60:     }
      61:     $file.CheckOut()
      62:     # reinstantiate web part manager after file was checked out in order
      63:     # to avoid File is not checked out error
      64:     $webPartManager = $file.Web.GetLimitedWebPartManager($file.Url,
      65: [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
      66:     $webParts = @()
      67:     foreach ($wp in $webPartManager.WebParts)
      68:     {
      69:         Write-Host "Found web part" $wp.GetType().Name
      70:         if ($wp.GetType().FullName -eq
      71: "Microsoft.Office.Server.Search.WebControls.ContentBySearchWebPart"
      72:         {
      73:             $webParts += $wp
      74:         }
      75:     }
      76:  
      77:     if ($webParts.Count -eq 0)
      78:     {
      79:         return
      80:     }
      81:  
      82:     $webParts | ForEach-Object { Fix-Content-By-Search-Web-Part $_ $webPartManager }
      83:  
      84:     Write-Host "Update and checkin page" $file.Url -foregroundcolor green
      85:     $file.Update()
      86:     $file.CheckIn("Change fallback language for content by search web parts to $lcid.")
      87:     if ($shouldBePublished)
      88:     {
      89:         Write-Host "Publish page" $file.Url -foregroundcolor green
      90:         $file.Publish("Change fallback language for content by search web parts to $lcid.")
      91:         if ($file.DocumentLibrary.EnableModeration)
      92:         {
      93:             $file.Approve("Change fallback language for content by search web parts to $lcid.")
      94:         }
      95:     }
      96: }
      97:  
      98: function Fix-Content-By-Search-Web-Parts-In-Web($w)
      99: {
     100:     Write-Host "Check pages on web" $w.Url -foregroundcolor green
     101:  
     102:     $pw = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($w)
     103:     $pagesList = $pw.PagesList
     104:     $pagesList.Items | ForEach-Object { Fix-Content-By-Search-Web-Parts-On-Page $_ }
     105: }
     106:  
     107: if (-not $siteColl)
     108: {
     109:     Write-Host "Specify site collection url in siteColl parameter" -foregroundcolor red
     110:     return
     111: }
     112:  
     113: if (-not $lcid)
     114: {
     115:     Write-Host "Specify target locale id (integer) in lcid parameter" -foregroundcolor red
     116:     return
     117: }
     118:  
     119: $site = Get-SPSite $siteColl
     120: $site.AllWebs | ForEach-Object { Fix-Content-By-Search-Web-Parts-In-Web $_ }