Search This Blog

Saturday, May 11, 2013

Display Ratings control inside Data View Web Part (DVWP - SharePoint Designer) aka Data Form Web Part (DFWP)

Though this looks straightforward i.e. drag and drop the rating field from data source in data view web part and it will show the rating stars (asterisks) but the output does not show as expected in data form web part.

There are few posts on internet to create a custom code data view web part that can display the ratings (by inheriting from data view web part). Well how about doing it out of the box. Yes - there is a 3 step solution  we have and that is what this post is about. So here we go:

1.  Modify the web.config file for SharePoint web application. Open web.config in a editor and
search for <Controls> section. There is already one line there for We add another one as below:

  <add tagPrefix="Portal" namespace="Microsoft.SharePoint.Portal.WebControls" assembly="Microsoft.SharePoint.Portal, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />

Why we do the above: Data View Web Part does not use the import / register tags defined on top of .aspx page.  It has its own independent system (built within its compiled code) to use the namespace it wants to use. By adding the above line in <Controls> section it makes those namespaces available.

2. In SharePoint Designer open the page containing data view web part
    Switch to code view: On the top of DVWP in xslt, there are all the namespaces that are defined.
    We will add the namespace so that we can use any control from

    "Microsoft.SharePoint.Portal.WebControls" defined in controls section in step 1) above


Why : This is because for any dll methods we want to use in DVWP, it must define the namespace for that in XSLT.

3. Finally to display the ratings controls add the following tag in the XSLT wherever we need to display the ratings:

<Portal:AverageRatingFieldControl  runat="Server" itemid="{@ID}"  id="MyRating{generate-id()}" FieldName="AverageRating" ControlMode="Edit" />

Few things above: 
a) {@ID} links the ID of the listitem for which we want to display or display/edit the ratings
b) FieldName must be AverageRating only
c) ControlMode: If we want to only display the ratings use "Display" however for display as well as allow  user to modify the rating use "Edit"

a) The user profile service must be is running since rating data is stored in Social Database, and ups should be associated with the web application.
 b) The Users Profile Social .... timer services must be running (2 of those). By default the update time is in hours i.e. when user clicks the asterisk, it would take few hours to show the updated rating. Change the settings to make it update faster (the downside of running more frequently means more load on server).

Hope this will be useful to anyone looking to get ratings without writing any code.

 -- Mohan