Search Guys

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

Updating SP2013 refiners using PowerShell

Updating SP2013 refiners using PowerShell

  • Comments 7
  • Likes

In SharePoint 2010, changes to the refiners were made using the the FilterCategoriesDefinition property of the refiners web part which held an XML blob.  In SharePoint 2013 we instead do this using the SelectedRefinementControlsJson property.  As the name implies, the configuration is now in JSON format.  Conveniently PowerShell 3.0 now includes the ConvertTo- and ConvertFrom-Json cmdlets that make this a breeze.  Here’s a code snippet that changes the display name for the Last Modified Date refiner to “Mod Time” (all relevant error handling removed for clarity):

# Get our page and check it out
$spweb = Get-SPWeb "http://da-server/search/"
$page = $spweb.GetFile("Pages/results.aspx")
$page.CheckOut()

# Find the Refinement web part
$webPartManager = $spweb.GetLimitedWebPartManager("http://da-server/search/Pages/results.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$rwp = $webpartmanager.webparts | ? { $_.title -eq 'Refinement' }

# Refiners are updated by changing the JSON
$j = $rwp.SelectedRefinementControlsJson | ConvertFrom-Json
$j.refinerConfigurations | % { if ($_.propertyName -eq 'LastModifiedTime') { $_.displayName = 'Mod Time'; }
$rwp.SelectedRefinementControlsJson = ConvertTo-Json $j -Compress

# Save our changes
$webpartmanager.SaveChanges($rwp)          # save changes to webpart
$page.CheckIn('Changed last mod refiner') $page.Publish('Changed last mod refiner')

Now, after reloading my search results page, I see the new display name:

modtime

Comments
  • Excellent stuff. Thanks for sharing.

  • Hi

    All commands run successfully, I have done crawl again. but still can't see the difference.

  • got it ..  one line is missing in above snippt. under

    # Save our chnages

    $webpartmanager.SaveChanges($rwp)                    # save changes to webpart.

    Thanks Matt.. you post solved my problem.

  • You're missin "}" in line:

    $j.refinerConfigurations | % { if ($_.propertyName -eq 'LastModifiedTime') { $_.displayName = 'Mod Time'; }

    at the end.

    Besides trying to convert it back to JSON gives:

    Exception setting "SelectedRefinementControlsJson": "No parameterless constructor defined for type of 'Microsoft.Office.Server.Search.WebControls.RefinementControl[]'."

  • Answered the question on stackexchange http://stackoverflow.com/questions/21410536/sharepoint-2013-selectedrefinementcontrolsjson-throwing-error/21604716#21604716

  • Nice post!
    I'd like to add a new refiner to this web part.
    How would you do it via PowerShell?

  • Carolina Bauque: Were you able to achieve adding new refiner to a web part. if yes could you please share the code

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