Thursday, July 30, 2009

SharePoint Custom Search using FullTextSqlQuery – Format Hit Highlighted Summary

SharePoint search uses “HitHighlightedSummary “column for highlighting the search text in result summary. Querying the HitHighlightedSummary column using FullTextSqlQuery returns the data that contains hit highlights that are surrounded with tags like <c0>…</c0>

This hit-highlight can be easily customized in OOB search core result web part by customizing the xsl. If it is a custom web part and using FullTextSqlQuery then we need to write piece of code to format hit-highlights.

public static string FormatHitHighlightedSummary(object text, string highlightedTag)
{
return Regex.Replace(Regex.Replace(text.ToString(), @"<c\d>", string.Format("<{0}>", highlightedTag), RegexOptions.IgnoreCase), @"</c\d>", string.Format("</{0}>", highlightedTag), RegexOptions.IgnoreCase);
}


While binding the Highlighted Summary, use the above created method as shown below
<%# FormatHitHighlightedSummary(Eval("HitHighlightedSummary"), "i")%>

I’ve used “Italics - <i>” to Highlight the search text… Use this method to format highlighted summary depends on your requirement.

No comments: