Displaying content rating in transformations
Rating controls can also be displayed in transformations. Place the following code in your transformation, ensuring that the ~/CMSAdminControls/ContentRating/RatingControl.ascx control is displayed along with the transformed item:
<%@ Register Src="~/CMSAdminControls/ContentRating/RatingControl.ascx" TagName="RatingControl" TagPrefix="cms" %>
<cms:RatingControl ID="elemRating" runat="server" Enabled="true" RatingType="Stars" ExternalValue='
<%# Convert.ToString(CMS.Helpers.ValidationHelper.GetDouble(Eval("DocumentRatingValue"), 0)/((CMS.Helpers.ValidationHelper.GetDouble(Eval("DocumentRatings"), 0) == 0?1:CMS.Helpers.ValidationHelper.GetDouble(Eval("DocumentRatings"), 1)))) %>' />
With this control, you can use similar properties as with the Content rating web part, as described in Using the Content rating web part:
- bool Enabled - indicates if rating is possible via the control. If disabled, the control is visible, but rating is not possible.
- bool CheckPermissions - the same as the Check permissions web part property.
- bool HideToUnauthorizedUsers - the same as the Hide to unauthorized users web part property.
- bool CheckIfUserRated - the same as the Check if user rated web part property.
- bool AllowForPublic - the same as the Anonymous users can rate web part property.
- string ErrorMessage - the same as the Error message web part property.
- string MessageAfterRating - the same as the Message after rating web part property.
- string ResultMessage - the same as the Result message web part property.
- bool ShowResultMessage - the same as the Show result message web part property.
- string RatingType - the same as the Rating type web part property.
- string ExternalValue - similar as the Rating value property.
- bool AllowZeroValue - the same as the Allow zero value property.
- int MaxRatingValue - the same as the Max rating value property.
Ratings submitted via this control are added to the ratings of the currently displayed document, the same as if you rated via the Content rating web part.
You can use the content rating and other controls only in transformations of type ASCX. The rating control will not be rendered correctly by other transformation types.
Example - Adding news ratings
The following example demonstrates how to add the rating control to your pages via transformations. We will use the sample Corporate Site and add the rating functionality to news items displayed in the News section. A similar result can be achieved using the Content rating web part, as described in Using the Content rating web part.
Open the Pages application.
Select the News page in the content tree.
View the page on the Design tab and configure (double-click) the NewsRepeater web part.
In the Web part properties dialog, click Edit next to the Selected item transformation property.
Switch the Transformation type to ASCX.
Replace the original transformation with the following code:
<%@ Register Src="~/CMSAdminControls/ContentRating/RatingControl.ascx" TagName="RatingControl" TagPrefix="cms" %> <div class="newsItemDetail"> <h1> <%# Eval("NewsTitle") %></h1> <div class="NewsSummary"> <%# IfEmpty(Eval("NewsTeaser"), "", GetImage("NewsTeaser")) %> <div class="NewsContent"> <div class="Date"> <%# GetDateTime("NewsReleaseDate", "d") %></div> <div class="TextContent"> <%# Eval("NewsSummary") %></div> </div> <div class="Clearer"> </div> </div> <div class="NewsBody"> <div class="TextContent"> <%# Eval("NewsText") %></div> <cms:RatingControl ID="elemRating" runat="server" Enabled="true" RatingType="Stars" ExternalValue=' <%# Convert.ToString(CMS.Helpers.ValidationHelper.GetDouble(Eval("DocumentRatingValue"), 0)/((CMS.Helpers.ValidationHelper.GetDouble(Eval("DocumentRatings"), 0) == 0?1:CMS.Helpers.ValidationHelper.GetDouble(Eval("DocumentRatings"), 1)))) %>' /> </div> </div>
Save the changes.
Now if you go to the live site, browse to the News section and display the details of a news item, you see the rating control present below the news text.