Creating a custom search results page for a (searchresults.aspx)

When creating a site using Windows Sharepoint Services, search is typically managed at the web application level.  This means that although the pages you create for your site will inherit the styles and layout designated in your site masterpage, any search results will be displayed using the default web application theme. By taking the following steps, it is possible to customise the way search results are shown, without affecting other sites operating in your WSS web application.

Create a custom searchresults aspx page

When you perform a sharepoint site search, the search results are displayed using the searchresults.aspx page.  This is located in C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS.  Copy the contents of this file.  Now, navigate to your site (using sharepoint designer), and create a new aspx page named ‘CustomSearchResults.aspx’.  Paste the contents of searchresults.aspx into your newly created page. At this point, you need to make two changes to the page.
Firstly, update the Page directive to point to your site masterpage. 
For ie, <%@ Page language="C#" MasterPageFile="~masterurl/default.master"  ... %>
In order to ensure that this is set out correctly, it is best to copy this from another page in your site that inherits from your site masterpage.
Secondly, remove unnecessary controls from the page.  There will be a number of controls registered at the top of the page; it is likely that these are already registered in your masterpage.  Therefore, remove all except the following controls:

<%@ Register Tagprefix="SharePoint" Namespace=
"Microsoft.SharePoint.WebControls" ...  %> <%@ Register Tagprefix="SearchWC" Namespace=
.Internal.WebControls"  ... %>
<%@ Register Tagprefix="WebPartPages" Namespace=
"Microsoft.SharePoint.WebPartPages" ... %>
Create a custom searchresults control
Your customised search results page is now inheriting from your masterpage.  In order to display the results of a given search, it is necessary to create a customised control to place on your masterpage. Navigate to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES and locate SearchArea.ascx.  This is the control which displays the search bar on your site, and passes the search parameters supplied to the search results page.  Again, copy the contents of this file, and create a new CustomSearchBox.ascx control (this time, in the CONTROLTEMPLATES directory) with the pasted contents of SearchArea.ascx.
Open CustomSearchBox.ascx and make the following changes:

Firstly, update the referenced url to reflect your customised search results page, near the top of the page.

string strEncodedUrl= SPHttpUtility.EcmaScriptStringLiteralEncode(SPHttpUtility.UrlPathEncode(web.Url + "/CustomSearchResults.aspx", false, false));

Secondly, update the names of the searchscope and searchstring, near the bottom of the page.

<SELECT id='idSearchScope' name='CustomSearchScope' class='ms-searchbox' ... >>
<INPUT Type=TEXT id='idSearchString' size=35 name='CustomSearchString' display='inline'  ... >>

And, thirdly, update the javascript calls to reflect a custom search function.

<INPUT Type=TEXT id='idSearchString' size=35 name='CustomSearchString'  ... onKeyDown="return
CustomSearchKeyDown(event, <%=strEncodedUrl%>);"  ... >
<a target='_self' href='javascript:' onClick="javascript:
SubmitCustomSearchRedirect(<%=strEncodedUrl%>);javascript:return false;"  ... > </a>

Edit Core.js

In order to redirect the page to your custom search results page, we need to create a couple of custom javascript functions.  Navigate to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\1033, and open the Core.js file.  Search for the “SubmitSearchRedirect” function.  This is the default search function.  Unfortunately, the Form actions at the end cause sharepoint to require a screen refresh each time you navigate to your custom search results page.  To solve this, we can create a new function, as follows: (the new function ensures existing site search will remain unaffected)
function SubmitCustomSearchRedirect(strUrl)
                var frm=document.forms["frmSiteSearch"];
                var searchText=frm.elements["CustomSearchString"].value;
                var searchScope=frm.elements["CustomSearchScope"].value;
                window.location = strUrl;
Likewise, search for the SearchKeyDown function, duplicate it, and edit as follows:
function CustomSearchKeyDown(event, strUrl)
                if (IsKeyDownSubmit(event))
                                return false;
                return true;

Adding your custom searchresults control to the site masterpage

You have now created a customised search control, which utilises a custom search results page.  In order to utilise this in your site, it is necessary to add the control to your masterpage.
This is a simple process, consisting of the following steps:
Firstly, register your control at the top of your masterpage.
<%@ Register TagPrefix="provoke" TagName="CustomSearchBox" src="~/_controltemplates/CustomSearchBox.ascx" %>
And, secondly, place the control at the appropriate location on your masterpage.
<asp:ContentPlaceHolder ID="ProvokeCustomSearch" runat="server">
<provoke:CustomSearchBox id="CustomPlaceHolderSearchArea" runat="server"/></asp:ContentPlaceHolder>
At this point, you should be able to run your site search (after a hard browser refresh), and view your customised search results page.

posted on 2007-10-19 18:11 AllenFeng 阅读(...) 评论(...) 编辑 收藏