March 15, 2007

Customizing search results page appearance in WSS 3.0

In this post I will share two tips about the customization of sharepoint search results appearance.
The results page is located in "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\searchresults.aspx". This is the page we will change.
First of all suppose we want to show 5 results page instead of the default 10. Open the aspx page and edit the SearchWC:CoreResultsWebPart.... webpart attributes by adding ResultsPerPage='"5". This should be enough.
As you may have noticed CoreResultsWebPart is a class showing results. Rather than duelling with the Reflector and writing my own class, handling the searches, the followng part was easier and much quicker for me. Secondly, what I wanted to accomplish was to remove the author and date information from the results pages. This was somewhat harder than the first part :). For this I have added another attribute to
SearchWC:CoreResultsWebPart, that is OnPreRender="PreRender_Handler".
Than add the following snippet



public void PreRender_Handler(object sender, System.EventArgs e)
{
CoreResultsWebPart coreResults = (CoreResultsWebPart)sender;
string searchResultsTxt = ((Literal)(coreResults.Controls[0])).Text;
Regex rex = new Regex(@"^\s*-\s*^\s*(\w*)\s*^\s*-\s*^\s*(\d+/\d+/\d+)",
RegexOptions.Multiline);
string replaced = rex.Replace(searchResultsTxt, string.Empty);
((Literal)(coreResults.Controls[0])).Text = replaced;

}



This code gets the search results html page and parses it with a regular expression , for cleaning the htmls of author and publishing date information. The Literal is the container for the html results. The regular expression gets the name and date in groups in case you may want to use them.
Hope it helps...