2

I need to edit the query of a Search Results web part on multiple pages of the same site collection (with no subsites). With the following code, I've been able to change the web part's title, but I can't find any properties on the web part related to the search query.

Add-PSSnapin Microsoft.SharePoint.Powershell

#Site on which web parts need to be changed
$siteURL = "http://example.com"
$pageURL = "/Pages/sample-page.aspx"
$Site = get-SPSite $siteURL

#load required assemblies to work with client context
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client”)
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client.Runtime”)

$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$page = $context.Web.GetFileByServerRelativeUrl($pageURL)
$webPartManager = $page.GetLimitedWebPartManager([Microsoft.SharePoint.Client.Webparts.PersonalizationScope]::Shared)
#$context.Load($page)
$context.Load($webPartManager.WebParts)
$context.ExecuteQuery()

$webpartDefinition = $webPartManager.WebParts[0]
$webpart = $webpartDefinition.WebPart
$webpart.title = "Changed Through PowerShell"
#$page.CheckIn()

$webpartDefinition.SaveWebPartChanges()

$context.ExecuteQuery()

How can I add to or change this code to replaced the selected web part's search query with my own?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.