How to display content rating in smart search transformations

   —   
This article describes how to use the content rating control in smart search result transformations and ensure that the proper value is loaded. The example in this article uses the default “CMS.Root.SmartSearchResultsWithImages” transformation.
As you already know from our Developer´s guide, there is a simple way to use the content rating control in transformations for standard documents. This option does not allow users to rate a document, as it only displays content rating results.

When you use the approach mentioned in the link above in a smart search result transformation, you will find that the rating control is visible, but the value is not shown. All controls are empty.

There are 2 problems this article deals with:

1. There is a problem with the life-cycle of this control when it is used within a smart search transformation. This can be fixed using the following code within the smart search result transformation:

<script runat="server">

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
elemRating.ReloadData();
}

</script>


2. Then, you need to change the default code of the control like this:

(default)
<cms:RatingControl ID="elemRating" runat="server" Enabled="false" RatingType="Stars" ExternalValue='<%# Convert.ToString(CMS.GlobalHelper.ValidationHelper.GetDouble(
Eval("DocumentRatingValue"), 0)/((CMS.GlobalHelper.ValidationHelper.GetDouble(Eval("DocumentRatings"), 0) == 0?1:CMS.GlobalHelper.ValidationHelper.GetDouble(Eval("DocumentRatings"), 1)))) %>' />


(new)
<cms:RatingControl ID="elemRating" runat="server" Enabled="false" RatingType="Stars" ExternalValue='<%# CMS.GlobalHelper.ValidationHelper.GetDouble(GetSearchValue("DocumentRatingValue"), 0)/((CMS.GlobalHelper.ValidationHelper.GetDouble(GetSearchValue("DocumentRatings"), 0) == 0?1:CMS.GlobalHelper.ValidationHelper.GetDouble(GetSearchValue("DocumentRatings"), 1))) %>' />

As you can see, the Eval function has been replaced by the GetSearchValue function, since Eval is bound to document data, but we need to get the correct value from the search index.

After these changes are made and the content rating control is registered on the page, the whole transformation code is as follows:

<%@ Control Language="C#" AutoEventWireup="true" Inherits="CMS.Controls.CMSAbstractTransformation" %>
<%@ Register TagPrefix="cc1" Namespace="CMS.Controls" Assembly="CMS.Controls" %>

<%@ Register Src="~/CMSAdminControls/ContentRating/RatingControl.ascx" TagName="RatingControl" TagPrefix="cms" %>

<script runat="server">

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
elemRating.ReloadData();
}

</script>

<div style="margin-bottom: 30px;">
<%-- Search result title --%>
<div>
<a style="font-weight: bold" href='<%# SearchResultUrl(true) %&gt;'&gt;
&lt;%# SearchHighlight(HTMLHelper.HTMLEncode(DataHelper.GetNotEmpty(Eval("Title"), "/")),"&lt;span style=\"font-weight:bold;\"&gt;","&lt;/span&gt;") %&gt;
&lt;/a&gt;
&lt;/div&gt;
&lt;%-- Search result content --%&gt;
&lt;div style="margin-top: 5px; width: 590px;"&gt;
&lt;%# SearchHighlight(HTMLHelper.HTMLEncode(TextHelper.LimitLength(
HttpUtility.HtmlDecode(HTMLHelper.StripTags(GetSearchedContent(
DataHelper.GetNotEmpty(Eval("Content"), "")), false, " ")), 280, "...")),"&lt;span style=\"background-color: #F60;color:white;\"&gt;","&lt;/span&gt;") %&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;%-- Relevance, URL, Creattion --%&gt;
&lt;div style="margin-top: 5px;"&gt;
&lt;%-- Relevance --%&gt;
&lt;div title="&lt;%# "Trafność: " + Convert.ToInt32(ValidationHelper.GetDouble(Eval("Score"),0.0)*100) + "%" %&gt;"
style="width: 50px; border: solid 1px #aaaaaa; margin-top: 7px; margin-right: 6px; float: left; color: #0000ff; font-size: 2pt; line-height: 4px; height: 4px;"&gt;
&lt;div style='&lt;%# "background-color:#a7d3a7;width:"+ Convert.ToString(Convert.ToInt32((ValidationHelper.GetDouble(
Eval("Score"),0.0)/2)*100)) + "px;height:4px;line-height: 4px;"%&gt;'&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;%-- URL --%&gt;
&lt;span style="color: #008000"&gt;
&lt;%# SearchHighlight(SearchResultUrl(true),"&lt;strong&gt;","&lt;/strong&gt;")%&gt;
&lt;/span&gt;
&lt;%-- Creation --%&gt;
&lt;span style="color: #888888; "&gt;
&lt;%# GetDateTime(ValidationHelper.GetDateTime(Eval("Created"), DateTimeHelper.ZERO_TIME)) %&gt;
&lt;/span&gt;&lt;/br&gt;
&lt;cms:RatingControl ID="elemRating" runat="server" Enabled="false" RatingType="Stars" ExternalValue='&lt;%# CMS.GlobalHelper.ValidationHelper.GetDouble(GetSearchValue("DocumentRatingValue"), 0)/((CMS.GlobalHelper.ValidationHelper.GetDouble(GetSearchValue("DocumentRatings"), 0) == 0?1:CMS.GlobalHelper.ValidationHelper.GetDouble(GetSearchValue("DocumentRatings"), 1))) %&gt;' /&gt;
&lt;/div&gt;
&lt;/div&gt;


If the smart search is configured properly, content rating and its correct value will now be part of the smart search result page.
-rm-


See also: Smart search overview

Applies to: Kentico CMS 5.x
Share this article on   LinkedIn

Juraj Ondrus

Hi, I am the Technical support leader at Kentico. I'm here to help you use Kentico and get as much as possible out of it.