Wednesday, August 6, 2014

SharePoint 2013 Enterprise Search Go Back - Revisited

This is a rehash of my previous post on the same subject, but it's been redone to use images.

So you want to leverage SharePoint Enterprise Search because it has all the bells and whistles you are looking for in your results and search features. You set your site to leverage enterprise search and you let people start using it. The problem that comes up is that when a user begins using the search, they are taken to the Enterprise Search site collection and there is no quick way to get back from where they started. The only way, out the box, is to hit the back button until they are back to where they started.

I found this to be less than ideal, so I cobbled together some JavaScript that leverages a browser session cookie to capture the site they launch the search. When the user is ready to go back, they simply click on the back button to return to where they started.

Here is the javascript:

<script type="text/javascript">
function returnGoBackURL() {
//*******************Specify your url search site here (with https and ending forward slash)*********
var rootURL = '';
var lastURL = document.referrer;
if (lastURL.substr(0,rootURL.length) != rootURL) {
setCookie('lastURL', document.referrer, 1 );
document.getElementById('returnURL').href = document.referrer;
} else {
document.getElementById('returnURL').href = getCookie('lastURL');

function setCookie(c_name,value,exdays){
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value + ";path=/";
function getCookie(c_name) {
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
 if (x==c_name)
return unescape(y);
<a href=""><img border="0" src="/sites/searchFAST/SiteCollectionImages/home2.png" alt="home"></a>
<a id='returnURL' class='returnURL' href=''><img border="0" src="/sites/searchFAST/SiteCollectionImages/back.gif" alt="back"></a>

Here are the a couple of starter images for Home and Back. Just update the image references in the script to point to the location of these files after you have put them on your site.

Add the script to a file and put it in the site collection documents. You will need to add a content editor web part (CEWP) to each of the search result pages and point it to the file you saved with the above script.  I put the CEWP under Search Navigation.

In O365, the search site has a document library called 'Pages'. In here are a number of result pages used to display the search results. The default is 'results', but you will find others for 'peopleresults', 'videoresults' and some others. You will want add the content editor webpart to all the result pages where you want the back button to appear.


No comments:

Post a Comment