Re-Publish Implementing Content Ratings for Sharepoint via SP Designer-Part II Step by Step

 

Part I

Recently had a request for a step-by-step for how to implement content rating on an existing Sharepoint list.

First, create a separate list to store the ratings: name it content ratings.  Add a column for the rating using a dropdown that contains your possible rating scores i.e. 1, 2, 3, 4, 5

Add another column and name it sourceid-add this column as a linked column pointing to the list where you are storing the items that will eventually be rated and set it to use the source list’s id for the value.  Do the same for the source list, add a field named Rated and point it to the content rating list and choose #Rated as the value (This is an easy way to retrieve the #rated content items from the content rating list later-plus you can re-format this field on your source list to display the actual star in a subsequent step) Tip: If you plan on using this list to store ratings for several lists throughout your site then add specific id fields for each source list and follow a naming convention that makes sense for your implementation: i.e. for rated best practices I used bpid, for rated call questions I used cqid, for FAQ’s I used FQID etc.  This will help choose the appropriate content when aggregating data later. 

Next from your source list (the one you will be storing the items that will be rated) create another view from the list named "rated content" Then open the site in Sharepoint Designer and navigate to the rated content.aspx page in the list’s folder. 

Next, convert the DVWP on the page to XSLT-right-click the form control, convert to XSLT.  Next create a linked datasource to the content rating list-notice your data connection library in the right nav in SP Designer-there is a add linked datasource button available that allows you to essentially link two lists together-choose JOIN list when the option appears and you will have access to the fields in both lists and be able to query the content rating list for the aggregate of the ratings when ready.

Next, drop this javascript into the newitem form for the content rating list in placeholder main to prepopulate the sourceid field when users rate content-

The function to obtain the parameters is courtesy of the Microsoft SharepointDesigner team blog also included as part of the Fab 40 templates to pre-fill newitem forms via querystring params.

Next, customize your DVWP on your source list (where the rated content resides) to display both the star ratings and a link for users to rate (linked to the content rating page passing the id in the query string as a parameter)  Look at the XSL in part I Content Rating in my blog for a detailed example of how to use X-Path to return a row of data back from the content rating list based on the id of the particular list item in your soure list. 

From the row item template on a custom list contained with a DVWP in XSLT format:

<xsl:template name="dvt_1.rowview">
  <xsl:param name="ParentPath" />
  <xsl:param name="SrcPos" />
<xsl:variable name="dvt_1ParentRow" select="current()" />
  <xsl:variable name="FilteredRatedQuestions" select="../../../Call_Question_Ratings/Rows/Row[@QuestionID=$dvt_1ParentRow/@ID]" />

Once I had a handle to a proper rowset then I could perform whatever calculations I deem necessary to display the appropriate star ratings:

In my scenario a five star rating based on a simple average sufficed and used the images already available in the layouts directory.

<xsl:choose>

          <xsl:when test="../../../Call_Question_Ratings/Rows/Row/@QuestionID=$dvt_1ParentRow/@ID">
             <xsl:variable name="dvt_1RowRating" select="format-number(sum($FilteredRatedQuestions/@Rating)div (@_x0023_Rated), &quot;###0.0;-###0.0&quot;)"/>
            <xsl:if test="$dvt_1RowRating &gt;= ‘2’ and $dvt_1RowRating  &lt; ‘3’">
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
             </xsl:if>
             <xsl:if test="$dvt_1RowRating &gt;= ‘3’ and $dvt_1RowRating  &lt; ‘4’">
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
             </xsl:if>
             <xsl:if test="$dvt_1RowRating &gt;= ‘4’ and $dvt_1RowRating  &lt; ‘5’">
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>

             </xsl:if>
             <xsl:if test="$dvt_1RowRating &gt;= ‘5’ and $dvt_1RowRating  &lt; ‘6’">
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>
              <img src="\_layouts\images\addtofavorites.gif" border="0" align="absmiddle"/>

             </xsl:if>
         </xsl:when>
         <xsl:otherwise>
             <img src="\_layouts\images\addtofavorites.gif" border="0"/>
            </xsl:otherwise>

         </xsl:choose>

Finally, this is optional but I added another view to the content rating list named "Rated Content" and customized the DVWP to my liking and added a parameter to filter the listitems by the same parameter in the query string I passed to the content rating list.  This makes it easy to add a link to your source list where users can view all the ratings for a particular content item. 

Hope this is enough to get you going-

In Part III coming soon I will cover adding a custom sort expression using a variation of the x-path expression above to pre-sort the content list based on the aggregate rating. 

cheers!

Rod.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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