If you remember last little while I was working on customizing SharePoint 2010 ratings control, well, it’s time to write some more about adding rating control to other areas o f the site, for example SharePoint 2010 Search enter, Search Results to be specific. It would be a nice touch to have documents and list items in search results along with teir corresponding ratings, after all if you enabled your users to use rating – they will get used to the feature and would like to see it in more places and Search is first on the list.
Here is how you’d go about something like that:
1. Naigate to your search page and in the edit mode of your page select Edit Webpart on the Search Core Results WP
2. Now in the DisplayProperties -> Fetched Properties add the following item to the list of fetched columns:
<Column Name=”Rating”/>
3. Cick XSL Editor button right below the Fetched Properties field. You will see quite a bit of XML in the following window so paste it to Notepad and find the following line of code:
<xsl:call-template name=”DisplayAuthors”>
and paste the following code right above it. This will make sure the rating control appears right before the author information in the search results:
<xsl:choose>
<xsl:when test=”rating > 0″>
<span>
<xsl:attribute name=”title”>
<xsl:value-of select=”rating”/>
</xsl:attribute>
<xsl:call-template name=”stars”>
<xsl:with-param name=”starCount” select=”rating”/>
</xsl:call-template>
<xsl:if test=”round(rating) > rating”>
<img src=”/_Layouts/Images/Ratings/RatingsNew.png”/>
</xsl:if>
</span>
<br/>
</xsl:when>
<xsl:otherwise>
<b>Not Rated</b>
<br/>
</xsl:otherwise>
</xsl:choose>
4. Now scroll down the XML find the section where you see definitions of various templates, here is hwo we’re going to implement the template for the rating, paste the following:
<xsl:template name=”stars”>
<xsl:param name=”starCount”/>
<xsl:param name=”value” select=”1″/>
<xsl:if test=”$value <= $starCount”>
<img src=”/_Layouts/Images/Ratings/RatingsNew.png”/>
<xsl:call-template name=”stars”>
<xsl:with-param name=”starCount” select=”$starCount”/>
<xsl:with-param name=”value” select=”$value + 1″/>
</xsl:call-template>
</xsl:if>
</xsl:template>
5. Lastly, we need to provision te Rating search preoperty (column we’re going to grab a data from). In your central admin Service Applications (http://[central admin URL]/_admin/ServiceApplications.aspx) highligh Search Service Administration and click Manage on the ribbon.
6. Pefrom a full search crawl which you can initiate from Content Sources left nav link. Wait for the crawl to complete.
6. Pick Metadata Properties from the left nav menu and click New Managed Property from the newly opened page.
7. For name of the property type Rating of ty Decimal , then click Add Mapping
8. In the new modal box search for rating and pick column name that will be of the type of decimal.
9. Click OK to add new managed property.
That’s it. Now search results will include a image representing a rating for each search result and if some items have not been rated they will have “Not rated” instead.
Good luck!