Friday, April 10, 2015

Improving contextual Search in SharePoint Online / 2013

In April 2014 my Puzzlepart colleague Tarjei “The strongest man in SharePoint” Ormestøyl had a post about how to configure search settings for a site to redirect scope searches over to your search center instead of osssearchresults.aspx. (This would be the “search in this site” search box you find in sites.)
image
In my recent project we have implemented the same logic as we want scope searches in SharePoint 2010 to be sent over to the new 2013 farm.

I have improved on Tarjei’s solution with the following points:
  • Display title of the site instead of just the URL (or list title as we support 2010 redirects to 2013 search)
  • Implement as a generic script to be added anywhere on the page – preferably in the control template, but for this post I’ll do it as a script web part.
If you look at the code I’m using a neat trick to get the DataProvider for the page and also hook in my logic after the results are ready the first time to fire off my custom scope hint code.

// Get a reference to the DataProvider for the page
var dp = Srch.ScriptApplicationManager.get_current().queryGroups.Default.dataProvider;
// Add event to be called after results are ready
dp.add_resultReady(showScopeHint);


The advantage by using the search center instead of osssearchresults.aspx is that you can control which refiners you show and tap into whatever query rules you have defined on your search center site collection (because I know you have a separate site collection for your search center right?).

If you pass in the u= parameter to a search center you will get results from that scope only, but no UI indication.

image

With my added script it looks like the image below, where the site title is hyperlinked with the scope URL, providing a way to navigate back to the site collection you searched from.

image

You can add the script via your master page, as a content editor web part or a script editor web part. Note that it requires jQuery for ajax calls and DOM manipulation.

Get it from the SPCSR Github repository.

And to set up contextual search on a site collection and point it to your search center.
  • Go to _layouts/15/enhancedSearch.aspx?level=sitecol
  • Enter the URL to your search center as Search Center URL
  • Enter {SearchCenterURL}/results.aspx?u={contextUrl} as the custom page URL

image

No comments:

Post a Comment