Add Custom Search box in SharePoint 2010

Adding your own Custom controls like search box or quicklaunch bar and replacing the existing ones, is infact a pretty easy task in SharePoint 2010. All you need to do is to create and install a feature that specifies your own custom control and give a lower sequence number for in your elements.xml file. Because of its lower sequence number than the already existing control, the corresponding Delegate control (which loads the Out-of-box control) will pick up your custom control.

Lets see the steps for replacing the Out-of-Box search box with the Custom Search Box control

1. Create a empty project in VS 2010.

2. Add a web user control (ascx file) in your project and write your logic for the custom search box. You can very well use the code behind files along with your ascx control, but don’t forget to change the ascx file’s Inherit tag to Inherit from the complied (and signed) .dll of the project.

3. Create a Feature -

. Add a new xml file to your project and name it as Fetaure.xml file . The content of the file will be something like below

<Feature Id=”541F5F57-C847-4e16-B59A-B31340E6F9E4A”
Title=”My Custom Search Box”
Description=”Replaces Out-of-Box search box with my Custom Search box.”
Version=”1.0.0.0″
Scope=”Web”
ReceiverAssembly=” OurCustomSearchControl, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”
ReceiverClass=”OurCustomSearchControl.SearchControl”
xmlns=”http://schemas.microsoft.com/sharepoint/”>
<ElementManifests>
<ElementManifest Location=” elements.xml”/>
</ElementManifests>
</Feature>

. Add another file in your project called elements.xml. Here we will use tag to specify our own custom search control box. The contents of the elements.xml file will be something like below :

<?xml version=”1.0″ encoding=”utf-8″ ?>
<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>
<Control
Id=”SmallSearchInputBox”
Sequence=”10″ -> Remember to give it a lower number than the existing SmallSearchInputBox control
ControlClass=”OurCustomSearchControl”
ControlAssembly=” OurCustomSearchControl Assembly”/>
</Elements>

4. Sign in and deploy.

Now, what happens behind the scenes?

Microsoft SharePoint Foundation implements a mechanism to declare candidate or out-of-box controls for a particular functionality by using a DelegateControl. he DelegateControl is embedded inside SharePoint Foundation pages and acts based on this information. So, in our case i.e. for the search box we should have  DelegateControl in our master page something like below

<SharePoint:DelegateControl ControlId=”SmallSearchInputBox”/>

At run time, this control accepts the union of control elements declared at the server farm, Web application, site collection, and Web site levels. The control that has the lowest sequence number is added to the control tree by means of the DelegateControl. In the case of a sequence tie, the order of controls is arbitrary.

Relate Post on other blog: Custom Search WebPart for Sharepoint 2010

Related Posts

  • Thomas Balkeståhl

    Hi.
    This is an excellent post, I will have great use for it.
    I have one request that would make it complete though, could you please add a description on how to add code from the existing searchbox or add a sample so that it is easier to get started?
    The line ‘and write your logic for custom search box.’ is what I’m stuck at currently.
    Thanks
    *****
    Thomas Balkeståhl – Technical Specialist – SharePoint

    • ikapoor

      hi,

      You should create a custom control that inherits from SearchBoxEx, if you need the out-of-box search capabilities of the SmallSearchInputBox control or else create a custom control and use fast search object model to modify the functionality of the control. Check the link http://msdn.microsoft.com/en-us/library/ee557874.aspx

  • http://codeforfuture.com Jay Patil

    You can also use the _spUserId variable that is available in all sharepoint pages derived from v4.master and most other master pages. This will give you the current user’s id if that is enough for you (can be used to save into list for e.g. if you pass “_spUserId;#” to the list, it will automatically find the user and save it.)

    To get current users’s name without a server call, you can use the text inside the Welcome control. For e.g. “Welcome, FirstName LastName”, you can parse and pick this text. This too works fine.

Disclaimer: The contents of this site are provided “AS IS”. Information and views expressed including URL(s) and other Internet website references, may change without notice. You bear the risk of using it. SharePoint, MOSS, SharePoint 2010, SharePoint 2013 and Microsoft are trademarks of the Microsoft Corporation.Learning SharePoint only provides Tips,Tricks and Tutorials for using Microsoft SharePoint, Microsoft Office and related Softwares.
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.