Managing a search sites and all their webpart settings can be a difficult process requiring many manual steps. Keeping the Fetched Properties and XSL of the Core Search Results webpart on the default search results page in sync between multiple environments can be time consuming and error prone. It’s bad enough to have to do this manually in a production environment, but add development, test, and stage environments to the mix and you end up with a lot of clicking and pasting; never a good way to arrive at a stable and repeatable process for your deployments. Luckily, PowerShell provides some relief to the copy, click, paste, repeat method of deploying search webpart customizations.
The following PowerShell script can read the values from an external XML or XSL file (which can now be placed under source control), check out the appropriate page (results.apsx in this case), update the property values on the web parts, check the page back in, and publish it:
function LoadXmlData([string]$filePath)
{
return [System.IO.File]::ReadAllText($filePath)
}
$executingFolder = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$resultsPageUrl = "http://localhost/sites/search/pages/results.aspx"
$siteUrl = "http://localhost/sites/search"$searchSite = new-object Microsoft.SharePoint.SPSite($siteUrl)
$searchWeb = $searchSite.OpenWeb()
$publishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($searchWeb)
$resultstpage = $publishingWeb.GetPublishingPage($resultsPageUrl)$resultstpage.CheckOut()
$webpartManager = $searchWeb.GetLimitedWebPartManager($resultstpage.Url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)foreach($webpart in $webpartmanager.WebParts)
{
switch ($webpart.Title)
{
"Refinement Panel"
{
write-host "Setting Filter Category Definition"
$webpart.FilterCategoriesDefinition = LoadXmlData(join-path $executingFolder "RefinerDefinition.xml")
}
"Search Core Results"
{
write-host "Setting Fetched Properties"
$webpart.PropertiesToRetrieve = LoadXmlData(join-path $executingFolder "FetchedProperties.xml")write-host "Setting XSL";
$webpart.Xsl = LoadXmlData(join-path $executingFolder "ResultView.xsl")
}
}
$webpartmanager.SaveChanges($webpart)
}$resultstpage.CheckIn("Automated Update");
$resultstpage.listItem.File.Publish("Automated Update")$publishingWeb.Close()
$searchWeb.Close()
$searchSite.Close()No more edit page, edit web part properties, save, check in, and publish in every environment. Simply copy the configuration files to the server and execute the script. Less chance for errors and less time spent deploying changes. The approach here can easily be expanded to manage much more than the three properties in the example.

0 comments:
Post a Comment