Jun
23
2008

(CodePlex) MOSS Faceted Search - HTML encode/decode problem fixed!

MOSS Faceted Search is a great SharePoint search extension project on CodePlex. It provides following features (copy from its CodePlex site)

  • Grouping search results by facet
  • Displaying a total number of hits per facet value
  • Refining search results by facet value
  • Update of the facet menu based on refined search criteria
  • Displaying of the search criteria in a Bread Crumbs
  • Ability to exclude the chosen facet from the search criteria
  • Flexibility of the Faceted search configuration and its consistency with MOSS administration

However, there are still some known issues exist due to this project does not update and release regularly.

HTML Encoding

An issue I experienced was HTML string encoding/decoding problem. After I added faceted search web part to search result page and searched for something, it returns result like following.

 

James Tsai .Net VSTO SharePoint Blog MOSS Faceted Search problem 

 

As you can see first facet value in both "Refine by Title" and "Refine by role" sections are empty. Only number of hits displayed. And it is because Faceted web part does not perform HTML encode/decode properly. It can be easily spotted by closer inspection of page's HTML source.


<!-- rendered html of facet web part -->


<td nowrap="nowrap" width="100%" style="vertical-align:middle"><a class="ms-navitem" href="javascript:__doPostBack(...)" style="vertical-align:middle;"><No Title></a><span> (2)</span></td>


<td nowrap="nowrap" width="100%" style="vertical-align:middle"><a title="&amp;lt;enter position title>" class="ms-navitem" href="javascript:__doPostBack(...)",style="vertical-align:middle;"><No Position defi...&lt;/a><span> (1)</span></td>

First empty facet value is causing by "<No Title>" rendered as html tag, which is not recognized by browser. And the second empty facet value is with half-open "<No Position defi..." also treated as malformed html tag.

To fix this problem, you have to modify Common\Templates.cs source code file. perform HttpUtility.HtmlEncode  for facet.DisplayName before assign it as link.Text

 

//Templates.cs source code

void link_DataBinding(object sender, EventArgs e)
{
            /******irrelevant code omitted*******/     
           

            if (facet.DisplayName.Length > _cropMax)
            {
                link.ToolTip = facet.Name;

                //HtmlEncode facet display name
                link.Text = HttpUtility.HtmlEncode(facet.DisplayName.Remove(_cropMax)) + "...";
            }
            else link.Text = HttpUtility.HtmlEncode(facet.DisplayName);  //HtmlEncode here too

            /******irrelevant code omitted*******/   
}

After you re-deploy fixed version of the feature and web part, the search result should now look like

 

James Tsai .Net VSTO SharePoint Blog MOSS Faceted Search fix encoding problem

 

Everything look fine now? If you now click on the link you just fixed as followed above steps. You will see this in returned refine search result page.

 

James Tsai .Net VSTO SharePoint Blog MOSS Faceted Search decoding problem

 

HTML Decoding

 

Yes, the problem is obvious. Whatever we encoded, we must decode it. Otherwise Faceted search web part will pass encoded query value to do refine search. And it will not return correct result (or no result at all, since encoded string will not look pretty).

To fix this problem you will need to modify Common\Utility.cs source code file. Do HttpUtility.HtmlDecode for query.Properties[property] before add it to keywords ArrayList.

 

public static string BuildQueryString(SearchQuery query)
{
             /******irrelevant code omitted*******/  

            foreach (string property in query.Properties)
            {
                // consider empty facets and don't add them to qs
                if (query.Properties[property] == Common.Constants.UNNAMED_FACET_VALUE) continue;
                ka.Add(string.Format("{0}:\"{1}\"",property, HttpUtility.HtmlDecode(query.Properties[property])));
            }

             /******irrelevant code omitted*******/  

            return qs;
}

 

If you followed all above steps you should now see correct refined search keyword and results.

 

James Tsai .Net VSTO SharePoint Blog MOSS Faceted Search decoding problem fix

blog comments powered by Disqus