Freigeben über


Updating SP2013 refiners using PowerShell

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 "https://da-server/search/"
$page = $spweb.GetFile("Pages/results.aspx")
$page.CheckOut()

# Find the Refinement web part
$webPartManager = $spweb.GetLimitedWebPartManager("https://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

  • Anonymous
    January 01, 2003
    Excellent stuff. Thanks for sharing.

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

  • Anonymous
    January 01, 2003
    The comment has been removed

  • Anonymous
    January 01, 2003
    Hi All commands run successfully, I have done crawl again. but still can't see the difference.

  • Anonymous
    November 20, 2013
    The comment has been removed

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

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

  • Anonymous
    January 08, 2015
    Thanks for the great post!

    In case it helps others, I was able to update the refiners using the following code:

    $newRefinerConfigurations = @()
    $newJsonObject = @{}

    $jsonString = @"
    {
    "sortBy" : 0,
    "sortOrder" : 0,
    "maxNumberRefinementOptions" : 15,
    "propertyName" : "",
    "type" : "Text",
    "displayTemplate" : "~sitecollection/_catalogs/masterpage/Display Templates/Filters/Filter_Default.js",
    "displayName" : "",
    "useDefaultDateIntervals" : false,
    "aliases" : [],
    "refinerSpecStringOverride" : "",
    "intervals" : null
    }
    "@

    $newRefinerJson = ConvertFrom-Json $jsonString
    $newRefinerJson.propertyName = ""
    $newRefinerJson.displayName = ""
    $newRefinerConfigurations += $newRefinerJson

    (add other refiners here..)

    $newJsonObject.refinerConfigurations = $newRefinerConfigurations

    $rwp.SelectedRefinementControlsJson = ConvertTo-Json $newJsonObject -Depth 3 -Compress

    $webpartmanager.SaveChanges($rwp)

  • Anonymous
    May 11, 2015
    Thanks very much