Pages

Thursday, January 17, 2013

Customize Search Box in SharePoint

Search in SharePoint is important to drive adoption and in SharePoint 2010 Enterprise search has improved and come a long way. People look to search for information rather than clicking through or navigating to find the information. SharePoint is an excellent Enterprise Content Management (ECM) tool because it is more a platform than simple tool Developers are given independence to take SharePoint to any level they want. In this blog we will see how we can customize search by creating custom controls and adding code. We will not be touching on adding best bets, and keywords to drive better results.

People always had requirement to customize the search box that is present in SharePoint out Of the Box (OOTB). This is definitely doable and to everyone's surprise it is lot easier to achieve this than many thing. We will see step by step in process of achieving this in this blog.

SharePoint has a nice delegate architecture where there is no need to modify the page for changing or modifying a certain functionality in the page. For example we can use delegate control to replace the OOTB search box with custom search box and perform the operation that we want to do in the search box. It is an unintrusive way of high jacking the functionality.
In order to replace the search box with custom control we fist have to create a project in Visual Studio.

Open Visual Studio and Create a Empty SharePoint Project




Once we have the project next step is to add Mapped folder "Control Templates". This is where the user controls go in SharePoint. We will be creating a user control similar to a search box and replace it.





Now we will add a user control called "CustomSearch.ascx" this is the control that we will use with delegate control to replace the existing search box.





Now I am adding a textbox and image with link similar to the OOTB search box. In order to show the difference I am adding the text as "Custom Search the site"

<input type="text" id="search_box" name="search_box" runat="server"  placeholder="Custom Search the site..." onkeypress="javascript: return DoPostBackOnEnterClick(event);" />
<a title="Search" id="goSearch" href="javascript:__doPostBack('search_go');">
<img id="cbl_search_go" src="/_layouts/images/gosearch15.png" alt="Search" class="srch-gosearchimg" style="border:1px solid #e3e3e3;" onmouseover="this.src='/_layouts/images/gosearchhover15.png'"
     onmouseout="this.src='/_layouts/images/gosearch15.png'"  /> </a>
I am setting all the attributes similar to the OOTB search box. obvisouly I am adding an postback on click using javascript and on enter click to mimic the OOTB as close as possible.
Now our user control is ready and we can now see how we can include this as a delegate control. In order to use this as delegate control we need to include it in an element file which we can deploy with a feature. So lets us create a element file. You can an element file by right click on the project and add a new item and select empty element file.





We now have the element file and we are almost done. All we have left to do is add this control and so we can deploy it (include it in the master page only if you have a custom Master Page).
<!—Custom Search Box --> 
  <Control Id="SmallSearchInputBox" Sequence="10"
     ControlSrc ="~/_controltemplates/CustomSearch.ascx">
  </Control>
This is the line that needs to be added in the element file to get it deployed. Note the important thing here is the sequence number and ID. I have given the sequence number of "10" which less than the OOTB sequence number and I am using the same ID as "SmallSeachInputBox".  The reason behind that is when this control gets deployed SharePoint loads all the controls as delegate control and since our sequence number is smaller this control will be loaded instead of OOTB control. Once we have the element file we can include this element file with any feature and deploy it and activate the feature.This is what you will see once you deploy this.



 Hope this blog is useful to you all and any feedback and comments are welcome.

Enjoy Coding and Customizing. SharePoint.