SP2013: Getting at the Search Results web part Search Criteria via PowerShell


Back in the SharePoint 2010 days to change the source of the Core Results Web Part you would simply set the Scope property on the web part object and be done with it.  In SharePoint 2013 we have query builders and result sources and things aren’t quite as straight forward.

First things first, lets get our web part object:

$siteUrl = "http://server/search"  # or whatever your site url is
$resultsPage = "Pages/results.aspx" # or whatever your search results page is
$resultsUrl = "$siteUrl/$resultsPage"

$spweb = Get-SPWeb $siteUrl

# Check out the page so we can make changes
$page = $spweb.GetFile($resultsPage)
$page.CheckOut()

# Get the web part manager for the page
$webPartManager = $spweb.GetLimitedWebPartManager($resultsUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

# And pull the web part we want to work on
$webpart = $webpartmanager.WebParts | ? { $_.Title -eq 'Search Results' }

So far this is working the same as it did in 2010.  The difference is that the 2013 search criteria is now stored inside the DataProviderJSON property:

PS C:\> ConvertFrom-Json $webpart.DataProviderJSON

QueryGroupName                        : Default
QueryPropertiesTemplateUrl            :
IgnoreQueryPropertiesTemplateUrl      : False
SourceID                              :
SourceName                            :
SourceLevel                           :
CollapseSpecification                 :
QueryTemplate                         : {searchboxquery}
FallbackSort                          :
FallbackSortJson                      : null
RankRules                             :
RankRulesJson                         : null
AsynchronousResultRetrieval           : False
SendContentBeforeQuery                : True
BatchClientQuery                      : True
FallbackLanguage                      : -1
FallbackRankingModelID                :
EnableStemming                        : True
EnablePhonetic                        : False
EnableNicknames                       : False
EnableInterleaving                    : True
EnableQueryRules                      : True
EnableOrderingHitHighlightedProperty  : False
HitHighlightedMultivaluePropertyLimit : -1
IgnoreContextualScope                 : False
ScopeResultsToCurrentSite             : False
TrimDuplicates                        : True
Properties                            :
PropertiesJson                        : {}
ClientType                            : AllResultsQuery
UpdateAjaxNavigate                    : True
SummaryLength                         : 180
DesiredSnippetLength                  : 90
PersonalizedQuery                     : False
FallbackRefinementFilters             :
IgnoreStaleServerQuery                : True
RenderTemplateId                      :
AlternateErrorMessage                 :
Title                                 :

Since this is the default search results page you’ll see that there is no Source set and the QueryTemplate is the default “{searchboxquery}”.

To change the source you’ll need the Name, Level and Id and I haven’t yet figured out how to find the source id.  So for now, we’ll just modify the QueryTemplate:

# Add a property restriction to the query template
$dataProvider = ConvertFrom-Json $webpart.DataProviderJSON
$dataProvider.QueryTemplate = "{searchboxquery} source:filesystem"
$webpart.DataProviderJSON = ConvertTo-Json $dataProvider -Compress

# Save the changes and check it back in
$webpartmanager.SaveChanges($webpart)
$page.CheckIn("Changed the Query Template")
$page.Publish("Changed the Query Template")

Now when we open up the web part editor and click on the “Change query” button we see our change:

image

 

Happy days are here again…

Comments (6)

  1. Aurélien says:

    Thx for theses tips !

  2. Anonymous says:

    Pingback from Quercus Blog » Items missing from Search Results in SharePoint 2013

  3. Anonymous says:

    Pingback from Quercus Blog » Items missing from Search Results in SharePoint 2013

  4. Anonymous says:

    Pingback from Quercus Blog » Items missing from Search Results in SharePoint 2013

  5. VanDusen says:

    Great Post! Thx.

  6. Piyush Raj says:

    Awesome, just what i needed. Thanks.!