Implementing “I Am Feeling Lucky” Like Google in SharePoint 2013 Search using JavaScript


Sathish Nadarajan
SharePoint MVP
Published On :   18 Nov 2013
Visit Count
Today :  1    Total :   13905
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


First of all, let us see what does “I Am Feeling Lucky” means to Google.

An I'm Feeling Lucky search means you spend less time searching for web pages and more time looking at them. You can also use I'm Feeling Lucky to find interesting content on the web you might not know about otherwise!

I'm Feeling Lucky links will appear when you hover your cursor over a prediction below the search box. Clicking this link will take you directly to the first webpage that returns for your query.

Now, let us see how we can implement the same on our SharePoint 2013. For that, the implementation will be somewhat like, creating a custom search text box and a Button. On the click of the button, let us do a SharePoint Search and redirect to the first link of the result set. That’s it. How easy this is. Let us see step by step.

Am creating an Empty WebPart page. On that, let us add a Content Editor Web Part and a Script Editor WebPart.

Content Editor WebPart is going to hold my Search text box and the Button.

Script Editor WebPart is going to hold my Scripts for the Search and Redirection. Its pretty straight forward.

Place the below code on the CEWP (Content Editor Web Part).

 <div>
 
 <label for="searchTextBox">Search: </label> 
 
 <input id="searchTextBox" type="text"/> 
 
 <input class="ms-SPButton" onclick="Search();" type="button" value="I Am Feeling Lucky"/> 
 
 <br/>
 
 </div>
 
 

This will display a label, textbox and a button.

On the Script Editor WebPart,

 <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script>
 <script type="text/javascript" src="http://sathishserver:20000/sites/SearchSampleDeveloperSite/SiteAssets/jquery-1.7.1.js"></script>
 <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
 <script type="text/javascript" src="/_layouts/15/sp.js"></script> 
 <script type="text/javascript" src="/_layouts/15/sp.search.js"></script>
 
 
 
 
 <script type="text/javascript">
      
 'use strict';     
 $(document).ready(function () {
 });
 function Search(){
 //Getting the ClientContext
 	var clientContext = new SP.ClientContext("http://sathishserver:20000/sites/SearchSampleDeveloperSite/");
 //Getting the Site
 		var contextSite = clientContext.get_site();
 //Creating an object for the KeywordQuery Class
 		var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext); 
 //Framing the Query Text.  Here you can give any CAML Query also.
 		keywordQuery.set_queryText($("#searchTextBox").val()); 
 //Creating an object for SearchExecutor		
 var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);  
 //Get the Results by Executing Query
 		this.results = searchExecutor.executeQuery(keywordQuery); 
 		clientContext.executeQueryAsync(Function.createDelegate(this, this.onSearchSuccess),Function.createDelegate(this, this.onSearchError));
 }
 
 function onSearchSuccess(sender, args){
 alert('Success');
 //Resolve the URL of the first result
 alert(results.m_value.ResultTables[0].ResultRows[0].Path);
 //Redirect to the Result URL
 window.location.replace(results.m_value.ResultTables[0].ResultRows[0].Path);
 }
 
 function onSearchError(sender, args){
 alert('Error');
 }
 </script>
 

The Code is inlined with the description. Since it is straight forward, there is not much to explain about this. Hope this helps and useful.

Happy Cooding.

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

SharePoint Analytics