Adding Ratings to the Search Results Page in SharePoint 2010


One of the most requested features for SharePoint 2010 was the ability to rate content. That functionality has been provided, but one place where it is lacking out of the box is Search.

A quick {insert search provider here} search will yield several very good results for how to add Ratings to Search, but unfortunately not one source was able to tell the whole story.

I intend to do so… ๐Ÿ™‚

High-Level Steps
1. Turn on ratings in at least one document library.
2. Add the managed metadata properties.
3. Customize the Advanced.aspx page.
4. Customize the Search Core Results Web Part on the Results.aspx page.
5. Customize the Refinement Panel Web Part on the Results.aspx page.
6. Search!

Enabling Ratings
1. Navigate to a document library.
2. From the ribbon, choose Library Settings.
3. Click Rating Settings.
4. Select Yes to enable ratings, click OK.
5. !Required! Run a Full Crawl
6. Proceed to the next section…

Adding the Managed Metadata Properties
1. Login to Central Administration > Manage Service Applications > Search Service Application.
2. Click Metadata Properties in the Quick Launch.
3. Click New Managed Property.
4. Enter Rating for the Property Name, select Decimal as the type, click Add Mapping and choose ows_AverageRating(Decimal) as the Crawled property; click OK.
5. Click New Managed Property.
6. Enter Rating Count for the Property Name, select Integer as the type, click Add Mapping and choose ows_RatingCount(Integer) as the Crawled property; click OK.
7. !Required! Run a Full Crawl

Customizing the Advanced Search Page
1. Go to your Search Center, and click Advanced.
2. Open the page in Edit mode.
3. Edit the Advanced Search Box Web Part and expand Properties, copy all of the text in the Properties field and paste it into an XML editor.
4. Add the following PropertyDef reference in the PropertyDefs section:

5. Add the following PropertyRef reference in the ResultTypes section:

6. Copy and Paste all of your XML back into the Properties field, save the web part and your page.

You should now be able to perform advanced searches using the Rating column:

Customizing the Search Core Results Web Part
1. In your Search Center, do a quick search to get to /Pages/Results.aspx.
2. Open the page in Edit mode.
3. Edit the Search Core Results Web Part and expand Display Properties, if this is your first edit of this web part, you will need to uncheck Use Location Visualization.
4. Edit the Fetched Properties by adding the two new Managed properties to this list:
and .
5. Click XSL Editor and copy/paste all of the code into an XML editor to customize the XSLT.
6. Insert the following code right before the

<div class="srch-Description2">

tag:

<!--Start Rating-->

                <div class="ratingDiv">

                <xsl:call-template name="FormatRating">  

                <xsl:with-param name="theRating" select="rating" />  

                <xsl:with-param name="theRatingCount" select="ratingcount" />

                </xsl:call-template>  

                </div>

<!--End Rating-->

7. Insert the following code right before the very last tag:

<xsl:template name="FormatRating">  

<xsl:param name="theRating" />  

<xsl:param name="theRatingCount" />  

<xsl:if test="$theRating > 0">  

<div style="display: inline-block; padding-left: 24px;">  

<div>  

<xsl:variable name="tempTitle" select="concat($theRating, ' Stars (')"/>  

<xsl:variable name="tempTitle2" select="concat($theRatingCount, ' Ratings)')"/>  

<xsl:variable name="ratingTitle" select="concat($tempTitle, $tempTitle2)"/>  

<xsl:attribute name="title">  

<xsl:value-of select="$ratingTitle"/>  

</xsl:attribute>  

<xsl:choose>  

<xsl:when test="round($theRating) = 0 and $theRating > 0">  

<xsl:attribute name="style">background-image:url(/_layouts/images/Ratings.png);cursor:default;height:16px;margin:0px;background-repeat:no-repeat;background-position:-368px 0px;width:16px;</xsl:attribute>  

</xsl:when>  

<xsl:when test="round($theRating) = 1 and round($theRating) &lt;= $theRating">  

<xsl:attribute name="style">background-image:url(/_layouts/images/Ratings.png);cursor:default;height:16px;margin:0px;background-repeat:no-repeat;background-position:-352px 0px;width:16px;</xsl:attribute>  

</xsl:when>  

<xsl:when test="round($theRating) = 2 and round($theRating) > $theRating">  

<xsl:attribute name="style">background-image:url(/_layouts/images/Ratings.png);cursor:default;height:16px;margin:0px;background-repeat:no-repeat;background-position:-352px 0px;width:32px;</xsl:attribute>  

</xsl:when>  

<xsl:when test="round($theRating) = 2 and round($theRating) &lt;= $theRating">  

<xsl:attribute name="style">background-image:url(/_layouts/images/Ratings.png);cursor:default;height:16px;margin:0px;background-repeat:no-repeat;background-position:-336px 0px;width:32px;</xsl:attribute>  

</xsl:when>  

<xsl:when test="round($theRating) = 3 and round($theRating) > $theRating">  

<xsl:attribute name="style">background-image:url(/_layouts/images/Ratings.png);cursor:default;height:16px;margin:0px;background-repeat:no-repeat;background-position:-336px 0px;width:48px;</xsl:attribute>  

</xsl:when>  

<xsl:when test="round($theRating) = 3 and round($theRating) &lt;= $theRating">  

<xsl:attribute name="style">background-image:url(/_layouts/images/Ratings.png);cursor:default;height:16px;margin:0px;background-repeat:no-repeat;background-position:-320px 0px;width:48px;</xsl:attribute>  

</xsl:when>  

<xsl:when test="round($theRating) = 4 and round($theRating) > $theRating">  

<xsl:attribute name="style">background-image:url(/_layouts/images/Ratings.png);cursor:default;height:16px;margin:0px;background-repeat:no-repeat;background-position:-320px 0px;width:62px;</xsl:attribute>  

</xsl:when>  

<xsl:when test="round($theRating) = 4 and round($theRating) &lt;= $theRating">  

<xsl:attribute name="style">background-image:url(/_layouts/images/Ratings.png);cursor:default;height:16px;margin:0px;background-repeat:no-repeat;background-position:-304px 0px;width:64px;</xsl:attribute>  

</xsl:when>  

<xsl:when test="round($theRating) = 5 and round($theRating) > $theRating">  

<xsl:attribute name="style">background-image:url(/_layouts/images/Ratings.png);cursor:default;height:16px;margin:0px;background-repeat:no-repeat;background-position:-304px 0px;width:80px;</xsl:attribute>  

</xsl:when>  

<xsl:when test="round($theRating) = 5">  

<xsl:attribute name="style">background-image:url(/_layouts/images/Ratings.png);cursor:default;height:16px;margin:0px;background-repeat:no-repeat;background-position:-160px 0px;width:80px;</xsl:attribute>  

</xsl:when>  

</xsl:choose>  

</div>  

</div>  

</xsl:if>  

</xsl:template>  

<!--End Rating-->

8. Copy and Paste all of your XML back into the XSL Editor, save the web part and your page.

1. In your Search Center, do a quick search to get to /Pages/Results.aspx.
2. Open the page in Edit mode.
3. Edit the Refinement Panel Web Part and expand Refinement.
4. Uncheck Use default configuration.
5. Copy/paste all of the code from the Filter Category Definition into an XML editor to customize the XSLT.
6. Modify one of the existing Category definitions, and modify it. Here’s what mine looks like:

7. Copy and Paste all of your XML back into the XSL Editor, save the web part and your page.

Search
1. Do a search for something which should yield content which you have rated.
2. You should not only see ratings IN the search result content, but you should be able to refine your results by the rating, COOL!

Important: I wouldn’t have gotten nearly this far without the following two sources:
1. http://todd-carter.com/post/2010/10/05/Pimp-Document-Ratings-within-SharePoint-Search-Results.aspx
2. http://blogs.technet.com/b/speschka/archive/2009/10/28/using-the-new-sharepoint-2010-ratings-feature-in-search.aspx

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s