Overview
In SharePoint 2010, a common requirement from end users was the ability to display rating stars in Content Query WebParts. A very popular and useful solution which was blogged about could be found here: (http://geeksthenewblack.wordpress.com/2010/10/29/show-rating-stars-in-the-content-query-web-part/) . This solution used XSLT to display the stars and render out a set of images depending on the number rated. So how can we get the same effect in the SharePoint 2013 CSWP, which uses javascript display templates to render its data?
The Problem
While the solution from SharePoint 2010 is a great one, it cannot be as easily translated into 2013 because of the way SharePoint 2013 handles its ratings images. In SharePoint 2010 it used the ratings.png file which looked like this:
Using this image and some simple css classes, you could easily display the rating how you required. SharePoint 2013 OOTB rating control displays the ratings in different manner, with different images and different HTML making things more interesting for us developers.
The Solution
To resolve this issue, I created a very simple javascript function that accepts the number of ratings and the average rating count as parameters. It returns the HTML formatted in the correct format used by the out of the box control. This javascript does not give you the ability to UPDATE the rating, but will enable you to display ratings in a consistent “SharePoint Styled” manner.
Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/// <summary>Generate Rating Stars</summary> /// <param name="rating" type="String"></param> /// <param name="ratingCount" type="String"></param> /// <returns type="String"></returns> function GenerateRatingStars(rating, ratingCount) { var totalStars = 5; var html = ""; if (ratingCount == "") { ratingCount = 0; } var filledImage = "/_layouts/15/images/RatingsSmallStarFilled.png"; var emptyImage = "/_layouts/15/images/RatingsSmallStarEmpty.png"; html += "<div class='ms-comm-noWrap'>"; html += "<span id='averageRatingElement-" + rating + "'>"; for (var i = 0; i < totalStars; i++) { var count = i + 1; var img = emptyImage; if (count <= rating) { img = filledImage; } html += "<span "; html += 'class="ms-comm-ratingsImageContainer"'; html += ">"; html += "<img "; html += 'id="averageRatingElement-' + rating + '-img-' + (i + 1) + '" '; html += 'src="' + img + '"/>'; html += "</span>"; } html += "</span>"; html += "<span class='ms-comm-ratingSeparator'/>"; html += "<span class='ms-comm-ratingCountContainer' id='averageRatingElement-" + ratingCount + "-count'>"; html += " " + ratingCount; html += "</span>"; html += "</span>"; html += "</div>"; return html; } |
Example Usage in Display Template
When using the code above in a display template, you need to add the required managed properties for RatingCount and AverageRating into your template so the properties can be retrieved. (Note: you may need to ensure that your managed properties are available in your Search Service)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!-- Ensure you use the following additional Managed Property Mappings --> <mso:ManagedPropertyMapping msdt:dt="string">'RatingCount':'RatingCount','AverageRating':'AverageRating'</mso:ManagedPropertyMapping> <!-- Add this below the <body> tag in your display template --> <script> $includeLanguageScript(this.url, "~sitecollection/Style Library/LISP/js/displaytemplate-ratings.js"); </script> <!--#_ var RatingCount = $getItemValue(ctx, "RatingCount"); var AverageRating = $getItemValue(ctx, "AverageRating"); var RatingDisplay = GenerateRatingStars(AverageRating, RatingCount); _#--> <!-- Use this in your code --> <span class="rating">_#= RatingDisplay =#_ </span> |
Output
If you have managed to add the items in correctly, then you will be presented with something that looks similar to this:
As you can see, they appear just like they would from the OOTB rating control and give your end users a nicer experience in their display templates.
Any problems let me know.
Hi Christopher,
Is it possible to make ratings interactive . Right now These are static images showing ratings.
Our SP site is our Intranet. We want to display the ratings as well as have them able to rate the document themselves. Is this possible?
Where/what is the template so that I can add the managed properties?
Thanks!
Michelle
Thanks very much! Saved me so much time.
The implementation is currently static only and there is no plan for me to write an interactive version of this. Thanks for visiting.