Monday, July 15, 2013

SharePoint 2013 Custom Search Box Without Visual Studio

So, I was working on a site design where the customer had a web part for doing employee lookups. Using the web part would basically direct you to the peopleresults.aspx page of the search center for the site. This site is being upgraded from 2010. I thought that there had to be a better way to do this so started doing some digging around to see what I could find.

It turns out that in 2013, there is a new search control that allows you to create a search box without using Visual Studio and you can do it in your master page. I found the line near the top of the master page that begins with:

<%@Register TagPrefix="SearchWC"

This line has the new Search control in it and allows for some nice customizing right in the declaration itself. So to create an "Employee Lookup" control I did this:

<SearchWC:SearchBoxScriptWebPart ID="EmployeeSearch" UseSiteCollectionSettings="false" InitialPrompt="Employee Lookup..." ShowPeopleNameSuggestions="true" ResultsPageAddress="/searchcenter/Pages/peopleresults.aspx" EmitStyleReference="false" ShowQuerySuggestions="true" ChromeType="None" UseSharedSettings="false" TryInplaceQuery="false" ServerInitialRender="true" runat="server" />

This creates a nice search box that goes directly to the desired page! It is clean and neat and does not take extra effort to create. It uses the same styles so can be styled however you want. The key options here are UseSharedSettings which must be false as well as UseSiteCollectionSettings.

The options available can be found on MSDN here

You can also extend this further if you did want to play with some templates and create a whole new experience. The templates are in the Master Page Gallery in the Display Templates/Search folder. The default template is called "Control_SearchBox_Compact" and there are 2 files (.js and .html) for each template. The ServerInitialRender property must be set to false if you want to override the templates!

I hope you have found this useful and please post any feedback you have!

Dan

7 comments:

  1. Brilliant post! I tried changing resultspage and couldn't understand why it didnt work untill I saw here that UseSiteCollectionSettings="false" was missing in my snippet. Thank you

    ReplyDelete
  2. Hi
    How can i hide this search box on my settings page? I am suing site master page as system master page and search box is available on all pages.

    ReplyDelete
  3. Thank you so much !!!
    You save my day !!
    Alex From FRANCE !!

    ReplyDelete
  4. Hi,

    I found this code in Searchresults.aspx and not in seattle.master/html.
    Can you please advice the right place to place the above code?

    Thanks,

    ReplyDelete
  5. Hi,

    I meant -"<%@Register TagPrefix="SearchWC"" found in searchresults.aspx and not in seattle.master.

    ReplyDelete
    Replies
    1. Priyanka,

      Unfortunately I don't have a copy of seatle.master to check at the moment. However, it is available in several master pages and the entire declaration can be copied. The main thing is that you need to be sure that you have the declaration in the master page at the top with all the others.

      Dan

      Delete