The Developer 2/2013

Page 29

<script> $includeScript(this.url, "~sitecollection/_catalogs/masterpage/Display Templates/Search/StarRating.js"); </script>

This is the contents of the StarRating.js file function RatingHtml(averageRating) { var ratingHtml = '<div class="starRating">'; var iAvg = parseInt(averageRating); var fAvg = parseFloat(averageRating); for(var i=0; i<iAvg; i++) // Full stars { ratingHtml+='<img src="/_layouts/15/images/RatingsSmallStarFilled.png"/>'; } if(iAvg<5) { if((fAvg-iAvg)>0) // Half star { ratingHtml+= '<img src="/_layouts/15/images/RatingsSmallStarLeftHalfFilled.png"/>'; } else { ratingHtml+='<img src="/_layouts/15/images/RatingsSmallStarEmpty.png"/>'; } if(iAvg<4) // Empty stars { for(var p=0; p<4-iAvg; p++) { // Empty stars ratingHtml+='<img src="/_layouts/15/images/RatingsSmallStarEmpty.png"/>'; } } } ratingHtml += '</div>'; return ratingHtml; }

And finally, render the AverageRating value on the page: <div id="starrating"> _#= RatingHtml(ctx.CurrentItem.AverageRating) =#_</div>

The last step is to assign your new display template to a new result type based on the result type for Word documents you get out of the box. It’s easy: just copy the outof-the-box result type for Word and tell it to use your new display template. This makes the search results show the average rating of your document:

CONCLUSION SharePoint 2013 gives you the powerful FAST search engine, and the choice between setting this up on-premises or using Office 365. In either case, you can customize search in a number of ways through display templates, configuration of search web parts, search schema mappings and query rules. You can also leverage custom document processing and custom ranking models. In this article we’ve shown you how to add the average rating of each document to the search results page. You can package your solutions as SharePoint Search Apps or in other end-points that utilize the power of rich object model APIs, REST/OData or SOAP. Whatever technology you choose and how you want to customize search, we hope to see you in the cloud. We’re all in!

29


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.