Include SharePoint 2010 rating in search results webpart

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

search core reults webpart

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 &lt;= $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!

This entry was posted in sharepoint, sharepoint 2010 and tagged , , , . Bookmark the permalink.

3 Responses to Include SharePoint 2010 rating in search results webpart

  1. andrewclark says:

    what version are you running? I noticed that your post does not mention that the fetched properties textbox is greyed out because the ‘Use Location Visualization’ checkbox is checked. If I play around and unmark that to add a property in the fetched properties text box, I get internal server errors. So I’m assuming you are running RC or something newer. btw, thanks for all the posts.

  2. Good point, I forgot to mention you have to un-check `Use Location Visualization`. I use RC and not getting an error, make sure you`re using the latest version with all of the patches etc. Also, good idea to check if your Fetched columns is not invalid XML (tags closed etc).

  3. andrewclark says:

    thanks for the confirmation. I’m not running RC because of a known issue but as soon as they fix it or release a newer version I’ll test out your code. (follow me on twitter & I’ll send you a DM about it: sharepointac) In the meantime, I’ll triple check my code. Thanks again!!!