Gravatar for coveoadmins@cir2.com

Question by CambridgeAdmins, Dec 8, 2016 12:33 PM

Functioning Hyperlink in Search Result Description

Is it possible to get a hyperlink to work in the search result description? We're trying to reduce the number of clicks our users have to make in order to get the information they need as quickly as possible. We have a SharePoint list filled with FAQs, and some of those contain links to supporting documentation. The hyperlinks work properly in the list itself, and in each item that contains a link. However, I noticed that when you search for the items in Coveo that the link in the description is only text. You have to open up the item in order to click on the link contained within it's fields.

This also seems to be true of really any document. If a Word document has a hyperlink inside of it, that link won't be usable from the search results. You have to open the document, because in the search results it appears as just plain text.

Is there anyway to make a hyperlink that shows up in a search result description usable from the search result page?

Any information on this would be greatly appreciated. Thank you for your time!

1 Reply
Gravatar for rquirion@coveo.com

Answer by ronald, Dec 8, 2016 2:46 PM

(Because of the ".net" tag, I'm assuming that the question is about the .NET UI.)

What do you mean exactly by "search result description"? Do you mean the result excerpt in which keyword highlighting may occur? If so, and assuming that the skin is using the default ResultExcerpt control, the simple answer to your question is: no, there is no such support in that result control. Its purpose is to show the document excerpt as it is in the index, so the content is HTML-encoded, including any URLs, so they become plain text.

Such feature could be implemented in a custom control though. I'm providing a sample here. To try it:

  1. Open the right ResultTemplate(_xxxxx.ascx) file in the skin folder.
  2. Replace the line <ces:ResultExcerpt id="exc" Factor="0.91" CssClass="CesResultExcerpt" runat="server"/> with <cnlb:BetterPlaceHolder ID="PHMyExcerpt" runat="server" />
  3. Add the following script block at the end of the file:

Script block:

<script runat="server"> 
    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        MyExcerpt myExcerpt = new MyExcerpt();
        myExcerpt.ID = "exc";
        myExcerpt.Factor = 0.91;
        myExcerpt.CssClass = "CesResultExcerpt";
        PHMyExcerpt.Controls.Add(myExcerpt);
    }

    public class MyExcerpt : ResultExcerpt {
    protected override void Render(HtmlTextWriter p_Writer)
    {
        // Call the base Render() method.
        System.IO.StringWriter buffer = new System.IO.StringWriter();
        HtmlTextWriter writer = new HtmlTextWriter(buffer);
        base.Render(writer);
        writer.Flush();

        // Replace textual hyperlinks with clickable hyperlinks.
        string htmlContent = buffer.ToString();
        foreach (string urlPrefix in new string[]{ "http://", "https://" }) {
            const string VALID_URL_CHARS = "-._~:/?#[]@!$&'()*+,;=`%";
            int startPos = htmlContent.IndexOf(urlPrefix);
            while (startPos != -1) {
                int endPos = startPos + urlPrefix.Length;
                while (endPos < htmlContent.Length &&
                        (char.IsLetterOrDigit(htmlContent[endPos]) || VALID_URL_CHARS.IndexOf(htmlContent[endPos]) != -1)) {
                    ++endPos;
                }
                string url = htmlContent.Substring(startPos, endPos - startPos);
                string urlSubst = "<a href=\"" + HttpUtility.HtmlDecode(url) + "\">" + url + "</a>";
                htmlContent = htmlContent.Substring(0, startPos) + urlSubst + htmlContent.Substring(endPos);

                startPos = htmlContent.IndexOf(urlPrefix, startPos + urlSubst.Length);
            }
        }

        // Write the result.
        p_Writer.Write(htmlContent);
     }
 }
 </script>

p.s. This sample has a known limitation: if highlighting occurs within a URL, it will be "cut".

p.s. I had to add extra spaces at a few places to have the text editor pleased.

Ask a question