Wednesday 28 May 2014

SharePoint 2010 - Navigation back to originating site/list when using a centralised search center

Heading to Site Actions -> Site Settings -> Site Collection Settings -> Search Settings,
there are options for configuring how search works.

A common scenario is when the Search results page is configured to direct the searcher to a centralised search center rather than the default, local '/_layouts/OSSSearchResults.aspx' page:


This is the norm if you have document management configured in your organisation, with centralised content types furnished managed metadata and this all brought together for the end user by using search to return documents across a number of different sites / site collections.

The issue many people run into is this.

The user is searching from their own site, or perhaps from within a document library on their site, and search, due to the configuration you have applied at the site collection level redirects them to another site collection (or perhaps web application) that is hosting the search center. Now this is all well and good, the documents or whatever are all returned in the results and everything appears to be working correctly.

But now, then want to get back to the site they search from or perhaps the list they searched from, but because search is on a different site altogether, all of a sudden there is no breadcrumb to direct them back, hence the user journey is a struggle, adoption is affected, etc.

So, we came up with a solution to this issue. Now this does involve jQuery, but don't let that scare you if you aren't using this on your environment yet, it's pretty easy and to be honest whenever you want to do anything on SharePoint that is a little bit clever and our of the norm you will usually need to use this fantastic library of functions you now have available to you.

(Now, i'm not going to go into how to add jQuery here, this is for a different post, which I will tackle shortly and provide you with a link.)

On your search results page(s), edit the page and add a plain old CEWP (content editor web part), choose 'edit html source' from the ribbon





Add the below code into the html editor and hit the save button. (Do note, the only thing that may vary here is the 'script src' which is the location of your jQuery library, in my instance, I have the library added to the folder in the hive of each of my WFE's in a Javascripts folder).

The way this works is by interrogating the URL, as this holds the source of the query, so when a user hit search from a site, the string 'This%20Site' is in the URL, and if they come from a list, yep you guessed it 'This%20List' is present. So all we're doing when these strings in the URL are found is taking slicing the 14 characters that depict the list or site, exscaping some unwanted characters and adding this as a HREF to a link that says either 'Back to Site' or 'Back to List'. Simples!

You may need to add some additional positioning in the <divclass="divlink" tag to make this appear where you want it.

<script src="/_layouts/javascripts/jquery/jquery-1.9.1.min.js" type="text/javascript"></script>

<script language="javascript" type="text/javascript">
 $(document).ready(function() {
$(".divlink").empty();
 
var url = document.URL;
var findlist = "This%20List";
var findsite = "This%20Site";
  
var listfound = url.search(findlist);
var sitefound = url.search(findsite);
 
 
if (listfound > 1) {
 
newurl = url.slice(listfound + 14); 
newurl = newurl.replace(/%2F/g,"/");
newurl = newurl.replace(/%3A/g,":");
newurl = newurl.replace(/&/g,"");
 
    var link = ("<a href=\"" + newurl + "\">Back to list</a>");
 
 
$(".divlink").append(link);
 
}
 
if (sitefound > 1) {
 
newurl = url.slice(sitefound + 14); 
newurl = newurl.replace(/%2F/g,"/");
newurl = newurl.replace(/%3A/g,":");
newurl = newurl.replace(/&/g,"");
 
 
 
    var link = ("<a href=\"" + newurl + "\">Back to site</a>");
 
  
 
$(".divlink").append(link);
 
}
 
 
});
​​​
</script>
<divclass="divlink" style="font-weight: bold; bottom: 45px"></div>




1 comment: