Search Guys

Everything search, including product commentary and guidance, search strategies and general technical guidance

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

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

  • Comments 6
  • Likes

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)

# 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
$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:



Happy days are here again…

  • Thx for theses tips !

  • Great Post! Thx.

  • Awesome, just what i needed. Thanks.!

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment