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) %>'>
<%# SearchHighlight(HTMLHelper.HTMLEncode(DataHelper.GetNotEmpty(Eval("Title"), "/")),"<span style=\"font-weight:bold;\">","</span>") %>
</a>
</div>
<%-- Search result content --%>
<div style="margin-top: 5px; width: 590px;">
<%# SearchHighlight(HTMLHelper.HTMLEncode(TextHelper.LimitLength(
HttpUtility.HtmlDecode(HTMLHelper.StripTags(GetSearchedContent(
DataHelper.GetNotEmpty(Eval("Content"), "")), false, " ")), 280, "...")),"<span style=\"background-color: #F60;color:white;\">","</span>") %><br />
</div>
<%-- Relevance, URL, Creattion --%>
<div style="margin-top: 5px;">
<%-- Relevance --%>
<div title="<%# "Trafność: " + Convert.ToInt32(ValidationHelper.GetDouble(Eval("Score"),0.0)*100) + "%" %>"
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;">
<div style='<%# "background-color:#a7d3a7;width:"+ Convert.ToString(Convert.ToInt32((ValidationHelper.GetDouble(
Eval("Score"),0.0)/2)*100)) + "px;height:4px;line-height: 4px;"%>'>
</div>
</div>
<%-- URL --%>
<span style="color: #008000">
<%# SearchHighlight(SearchResultUrl(true),"<strong>","</strong>")%>
</span>
<%-- Creation --%>
<span style="color: #888888; ">
<%# GetDateTime(ValidationHelper.GetDateTime(Eval("Created"), DateTimeHelper.ZERO_TIME)) %>
</span></br>
<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))) %>' />
</div>
</div>
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 overviewApplies to: Kentico CMS 5.x