Skip to content

Instantly share code, notes, and snippets.

@reshmee011
Last active October 3, 2016 16:26
Show Gist options
  • Save reshmee011/352aa388e1a8d9641cca28fffb8b6221 to your computer and use it in GitHub Desktop.
Save reshmee011/352aa388e1a8d9641cca28fffb8b6221 to your computer and use it in GitHub Desktop.
Add refiners to Refinement webpart
function GetWebPartByTitle()
{
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[Microsoft.SharePoint.Client.WebParts.WebPartDefinitionCollection] $webParts,
[Parameter(Mandatory=$true, Position=1, ValueFromPipeline=$true)]
[string] $webPartTitle
)
if($webparts.Count -gt 0){
Write-Host "Looping through all webparts"
foreach($wp in $webparts){
$ctx.Load($wp.WebPart.Properties)
$ctx.ExecuteQuery()
$propValues = $wp.WebPart.Properties.FieldValues;
if($propValues["Title"] -eq $webPartTitle)
{
return $wp;
}
}
}
return $null;
}
#return webpart based on title passed
function updateSearchRefinerWebPart
{
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[Microsoft.SharePoint.Client.ClientContext]$ctx,
[Parameter(Mandatory=$true, Position=1, ValueFromPipeline=$true)]
[string]$pageUrl
)
$web = $ctx.Web;
$page = $web.GetFileByServerRelativeUrl($pageUrl);
$ctx.Load($web);
$ctx.Load($page);
$ctx.ExecuteQuery();
#Check if page already checked out. If so, undo check out
if($page.CheckOutType -eq [Microsoft.SharePoint.Client.CheckOutType]::Online){
try{
Write-Host "Undo Checkout"
$page.UndoCheckOut()
$ctx.load($page)
$ctx.ExecuteQuery()
}
catch{
write-host "Error in checkout.. $($_.Exception.Message)" -foregroundcolor red
}
}
#If page is not checked out, then check out
if($page.CheckOutType -eq [Microsoft.SharePoint.Client.CheckOutType]::None){
Write-Host "Checkout"
$page.CheckOut()
$ctx.Load($page)
$ctx.ExecuteQuery()
}
# Find the Refinement web part
$lwpm =$page.GetLimitedWebPartManager([Microsoft.SharePoint.Client.WebParts.PersonalizationScope]::Shared);
$webParts = $lwpm.WebParts;
$ctx.Load($webParts);
$ctx.ExecuteQuery();
$rwp = GetWebPartByTitle -webParts $webParts -webPartTitle 'Refinement'
$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
}
"@
# Refiners are updated by changing the JSON
#$propValues = $_.WebPart.Properties.FieldValues;
$refctljson = $rwp.WebPart.Properties["SelectedRefinementControlsJson"] | ConvertFrom-Json
$newRefinerJson = ConvertFrom-Json $jsonString
$newRefinerJson.propertyName = "RefinableString00"
$newRefinerJson.displayName = "Directorate"
$refctljson.refinerConfigurations += $newRefinerJson
$refJson = ConvertTo-Json $refctljson -Depth 3 -Compress
$rwp.WebPart.Properties["SelectedRefinementControlsJson"] = $refJson
Write-Host "Checkin Page after adding refiner to refinement webpart"
# Save the changes
$rwp.SaveWebPartChanges();
$page.CheckIn('Changed refiner', [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$page.Publish('Changed refiner');
$ctx.load($page);
$ctx.ExecuteQuery();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment