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

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

$webpartDefinition = $webPartManager.WebParts[0]
$webpart = $webpartDefinition.WebPart
$webpart.title = "Changed Through PowerShell"



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.