SharePoint Pals
 | Sign In
Implementing “I Am Feeling Lucky” Like Google in SharePoint 2013 Search using JavaScript
by Sathish Nadarajan 18 Nov 2013
SharePoint MVP
Today  :   6     Total  :    12967

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).

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

This will display a label, textbox and a button.

On the Script Editor WebPart,

 <script type="text/javascript" src=""></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/"></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.
 //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){
 //Resolve the URL of the first result
 //Redirect to the Result URL
 function onSearchError(sender, args){

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.

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation