Updating SharePoint Content Search Web Part (CSWP) properties after Deployment

In SharePoint Custom solutions, the Content Search Web Parts (CSWP) might require minor property updates after the deployment, which is not feasible through UI. The typical example could be enabling/disabling of Cache. This could be cumbersome in case of updating multiple web parts across pages/sites.

Sometime there might be needs to asses the property values associated with web part's to validate the faulty behaviors. During such scenarios a script might be handy for a quick validation of the CSWPs.

I came across such situations during an engagement which forced me to write a PowerShell script which can help me to make the task much simpler.

Share the same for someone who may need this sometime.

This PowerShell script can be run on 2 modes (Read Only or Update) by setting the input parameter $saveChanges.

When executed on readOnly mode ($saveChanges = $false) the script will only find the pages and web parts where the changes are required and will NOT update any web parts.

Where as the Update Mode ($saveChanges = $true) will find the $strToFind and UPDATE the matches with the $stringToReplace.

$stringToFind = @('"Local SharePoint Search Results"') # $stringToFind = @('"TryCache":true','\"TryCache\":true')$stringToReplace = @('"Custom SharePoint Search Results"') # $stringToReplace = @('"TryCache":false','\"TryCache\":false')$siteUrl = "https://contoso.com:9996/test"$saveChanges = $false;

function UpdateSearchWebParts([Microsoft.SharePoint.SPSite] $site){    Write-Host "Updating Content search Webpart source string:"$stringToFind " with Target string " $stringToReplace    foreach($web in $site.AllWebs)    {            Write-Host "Validating web "$web.Url            $pagesList = $web.Lists.TryGetList("pages")            if ($pagesList -ne $null)            {                foreach($pageItem in $pagesList.Items)                {                    #Make sure you specify which page needs changes                    $page = $web.GetFile($pageItem.Url)                    Write-Host "Validating page "$page.Title "with Url"$pageItem.Url                                if ($page.CheckOutStatus -ne "None")                    {                        #Check to ensure the page is checked out by same user, and if so, check it in                        if ($page.CheckedOutBy.UserLogin -eq $web.CurrentUser.UserLogin)                        {

                            $page.CheckIn("Page checked in automatically by PowerShell script")

                            Write-Output $page.Title"("$page.Name") has been checked in"

                        }

                    }

                    if ($page.CheckOutStatus -eq "None")                    {                        $page.CheckOut()                        $wpm = $web.GetLimitedWebPartManager($page.Url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)                          foreach($wp in $wpm.WebParts)                        {                                     if ($wp.GetType().ToString().Contains("ContentBySearchWebPart") -eq $true)                            {                                write-host "Validating webpart "$wp.Title" on zone "$wp.ZoneID                                $json = $wp.DataProviderJSON;                                $newJson = $json;                                #write-host $json;                                $index = 0;

                                foreach($stf in $stringToFind)                                {                                    if ($newJson.Contains($stf) -eq $true)                                    {                                        $newJson = $newJson.Replace($stf, $stringToReplace[$index]);                                    }                                    $index++;                                }

                                #write-host $newJson;                                if ($json.Equals($newJson) -eq $false)                                {                                    write-host "Webpart "$wp.Title" on zone "$wp.ZoneID" requires update.." -ForegroundColor Yellow                                    if ($saveChanges -eq $true)                                    {                                        write-host "Updating webpart "$wp.Title" on zone "$wp.ZoneID                                        $wp.DataProviderJSON = $newJson;                                        $wpm.SaveChanges($wp)                                        write-host "Webpart "$wp.Title" on zone "$wp.ZoneID " updated successfully" -ForegroundColor Green                                    }                                }                                else                                {                                    write-host "No change required on Webpart" -ForegroundColor Green                                }                            }                        }                        $page.CheckIn(“Update via PowerShell”,[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)                        $page.Publish("Search webparts Updated by powershell")                    }                }            }        }}

$site = new-object Microsoft.SharePoint.SPSite($siteUrl)UpdateSearchWebParts -site $site

$web = $null;$site = $null;

UpdateContentWebpartProps.ps1