I came up with a requirement where I want the user to search the results only from the site collection level. In SharePoint 2010, we all know there are search scopes which help us to narrow down our search based on content sources, web address and metadata. However; there is no search scopes in SharePoint 2013. There is a new concept similar to Search Scope called as Search Result Sources.
This Result sources can be created at "Application Level", "Site Collection Level" and "Site Level".
You could find more information on Result Sources in SharePoint 2013 @ http://technet.microsoft.com/en-us/library/dn186229(v=office.15).aspx
I decided to script out the following
1. Creation of Enterprise Search Centre site
2. Update the search settings to point to this new search centre instead of default OSSSearchResults page.
3. Create a site collection level result source
4. Update the search results webpart properties to query only at above created site collection result source.
1. Create Enterprise Search Centre via PowerShell
Note: you need to activate the "BaseSite" Collection hidden feature before creating the search centre.
2. Settings the results page for your above search centre
3. Create a Site Collection level result source
4. Update the search results webpart to use this above result source for query.
Hope you like my approach.
enjoy!!
Isha Jain
This Result sources can be created at "Application Level", "Site Collection Level" and "Site Level".
You could find more information on Result Sources in SharePoint 2013 @ http://technet.microsoft.com/en-us/library/dn186229(v=office.15).aspx
I decided to script out the following
1. Creation of Enterprise Search Centre site
2. Update the search settings to point to this new search centre instead of default OSSSearchResults page.
3. Create a site collection level result source
4. Update the search results webpart properties to query only at above created site collection result source.
1. Create Enterprise Search Centre via PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Url = $(Read-Host -Prompt "Enter site url:") | |
if(-not(Get-PSSnapin | Where-Object {$_.Name -eq "Microsoft.SharePoint.PowerShell"})) { | |
Add-PSSnapin Microsoft.SharePoint.PowerShell; | |
} | |
$site = Get-SPSite $Url | |
Write-Host "Settings up search centre setup for your site" -ForegroundColor Yellow | |
$webs=$site.AllWebs | ? {$_.Url -like "*search*"} | |
$webs | % { Write-Host "Deleting old search centre" -ForegroundColor Yellow ; $_.Delete() } | |
Enable-SPFeature -Identity "BaseSite" -Url $site.Url | |
$searchSiteUrl= $site.Url+"/searchcentre" | |
New-SPWeb $searchSiteUrl -Name Search -Template SRCHCEN#0 -UseParentTopNav | |
Write-Host "Search centre is setup for your site successfully" -ForegroundColor Green |
Note: you need to activate the "BaseSite" Collection hidden feature before creating the search centre.
2. Settings the results page for your above search centre
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Write-Host "Settings results page for search centre for your site" -ForegroundColor Yellow | |
$SEARCH_SETTINGS_PROPERTY_NAME = "SRCH_SB_SET_WEB" | |
$settingsObj = @{} | |
$settingsObj.Inherit = $false | |
$settingsObj.ResultsPageAddress = "/searchcentre/pages/results.apsx" | |
$settingsObj.ShowNavigation = $false | |
$settingsObjAsJSON = ConvertTo-Json $settingsObj -Compress | |
if (!$web.AllProperties.ContainsKey($SEARCH_SETTINGS_PROPERTY_NAME)) | |
{ | |
Write-Host "- adding new entry " -ForegroundColor Yellow -NoNewline | |
$web.AllowUnsafeUpdates = $true | |
$web.AddProperty($SEARCH_SETTINGS_PROPERTY_NAME, $settingsObjAsJSON) | |
$web.Update() | |
} | |
else | |
{ | |
Write-Host "- updating entry " -ForegroundColor Yellow -NoNewline | |
$web.AllowUnsafeUpdates = $true | |
$web.SetProperty($SEARCH_SETTINGS_PROPERTY_NAME, $settingsObjAsJSON); | |
$web.Update() | |
} | |
Write-Host "Results page for search centre for your site is set succesfully" -ForegroundColor Green |
3. Create a Site Collection level result source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rite-Host "setting up the site collection level result souce for the site" -ForegroundColor yellow | |
$resultSourceName="MyResultSource" | |
$resultSourceDescription ="site collection result source" | |
$query="{searchTerms?} Path:{SiteCollection.Url}" | |
$ssa = Get-SPEnterpriseSearchServiceApplication "Search Service Application" | |
$fedman = New-Object Microsoft.Office.Server.Search.Administration.Query.FederationManager($ssa) | |
$searchOwner = Get-SPEnterpriseSearchOwner -SPWeb $web -Level SPSite | |
$resultSource = $fedman.GetSourceByName($resultSourceName, $searchOwner) | |
#Check To See if it exists | |
if(!$resultSource){ | |
$resultSource = $fedman.CreateSource($searchOwner) | |
} | |
$resultSource.Name =$resultSourceName | |
$resultSource.ProviderId = $fedman.ListProviders()['Local SharePoint Provider'].Id | |
$resultSource.Description = $resultSourceDescription | |
$resultSource.CreateQueryTransform($query) | |
$resultSource.Commit() | |
Write-Host "Settings the Site Collection scope for the search results in the Search Results webpart" -ForegroundColor yellow |
4. Update the search results webpart to use this above result source for query.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$resultSourceID=$resultSource.ID | |
$resultSourceLevel="SPSite" | |
$webURL =$web.Url + "/searchcentre" | |
$web=Get-SPWeb $webURL | |
$pageUrl="$webURL/Pages/results.aspx?k=test" | |
$page=$web.lists["Pages"].Items | ? {$_.Name -eq "results.aspx"} | |
$page.File.CheckOut(); | |
$webpartmanager=$web.GetLimitedWebPartManager($pageUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared) | |
$webpart = $webpartmanager.WebParts | ? { $_.Title -eq 'Search Results' } | |
$dataProvider = ConvertFrom-Json $webpart.DataProviderJSON | |
$dataProvider.SourceID=$resultSourceID | |
$dataProvider.SourceLevel=$resultSourceLevel | |
$dataProvider.SourceName=$resultSourceName | |
$webpart.DataProviderJSON = ConvertTo-Json $dataProvider -Compress | |
$webpartmanager.SaveChanges($webpart) | |
$page.File.CheckIn("Changed the Result Source") | |
$page.File.Publish("Changed the Result Source") | |
Write-Host "Site Collection scope for the search results in the Search Results webpart is set to 'MyResultSource' successfully" -ForegroundColor green |
Hope you like my approach.
enjoy!!
Isha Jain
No comments:
Post a Comment