The rating control in SharePoint 2010 as any other control in SharePoint automatically rendered depending on the type of the field in list or library. It`s very much bound to the data in the list item and cannot be functioning on its own. In other words I can`t rip out the control and place it somewhere on a Web Part or a custom ASPX page otherwise it will lose a context of item it`s bound to and won`t function properly.
Let`s take a look at the scenario where we have a custom list on a site which look is defined though XSL; we`ll go ahead and add our custom control to XSL and see how it renders on the page.
1. In your Visual Studio solution map the `Layouts` folder, and ensure you have `XSL` folder within it, if not – create one.
2. Add a new item to the root of the `XSL` folder of type XSLT located in the Data tab in `Add New Item` prompt.
3. Give the file a name `MyXSLSheet.xsl` or any other name as long as you keep XSL as an extension.
4. Open the file and replace its contents with the following code:
<xsl:stylesheet xmlns:x=”http://www.w3.org/2001/XMLSchema” xmlns:d=”http://schemas.microsoft.com/sharepoint/dsp” version=”1.0″ exclude-result-prefixes=”xsl msxsl ddwrt” xmlns:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime” xmlns:asp=”http://schemas.microsoft.com/ASPNET/20″ xmlns:__designer=”http://schemas.microsoft.com/WebParts/v2/DataView/designer” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:msxsl=”urn:schemas-microsoft-com:xslt” xmlns:SharePoint=”Microsoft.SharePoint.WebControls”><xsl:import href=”/_layouts/xsl/main.xsl”/><xsl:output method=”html” indent=”no”/><xsl:param name=”NoAJAX” select=”1″/><xsl:template mode=”Item” match=”Row[../../@TemplateType='100']“><xsl:param name=”Fields” select=”.”/><xsl:param name=”Collapse” select=”.”/><xsl:param name=”Position” select=”1″/><xsl:param name=”Last” select=”1″/><xsl:variable name=”thisNode” select=”.”/><table width=”100%” border=”0″ cellspacing=”0″ cellpadding=”0″ dir=”None”><tr><td width=”690″><h4><xsl:apply-templates select=”$Fields[@Name='LinkTitle']” mode=”PrintField”><xsl:with-param name=”thisNode” select=”.”/><xsl:with-param name=”Position” select=”$Position”/></xsl:apply-templates><xsl:apply-templates select=”$Fields[@Name='RatingCount']” mode=”PrintField”><xsl:with-param name=”thisNode” select=”.”/><xsl:with-param name=”Position” select=”$Position”/></xsl:apply-templates><xsl:apply-templates select=”$Fields[@Name='AverageRating']” mode=”PrintField”><xsl:with-param name=”thisNode” select=”.”/><xsl:with-param name=”Position” select=”$Position”/></xsl:apply-templates></h4></td></tr></table></xsl:template></xsl:stylesheet>
This template verifies that the type of list it will be applied to is a custom list (we know that by the Template ID = 100):
<xsl:template mode=”Item” match=”Row[../../@TemplateType='100']“>
Depending on your list you may have a different template ID which can always be looked up.
To look up template ID of the list open the following folder: [Drive]:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES where you will find the name of the feature representing the list you`re interested in, for example: CustomList.
The list will contain a definition folder with definition XML file in it that will contain template type attribute called `Type`. The ID of the type is something you would use in your template to ensure it`s being applied to a correct list type.
In case you`re applying a template to just one list or library – you may consider not verifying Template ID since none of the other instances of lists or libraries will be using it.
The section that actually renders average field and rating count field is this:
<xsl:apply-templates select=”$Fields[@Name='RatingCount']” mode=”PrintField”><xsl:with-param name=”thisNode” select=”.”/><xsl:with-param name=”Position” select=”$Position”/></xsl:apply-templates>
Above the names of the fields are rendered on a page.
Let`s now apply the template to our list:
1. Navigate to the root of your test SharePoint site.
2. Create a custom list on the site.
3. Click on the link that will take you to the `All Items` view of the list.
4. Click Site Actions -> Edit Page.
5. Access Web Part properties window of the list view.
6. In Miscellaneous section, locate XSL link property and set its value to `/_layouts/XSL/MyXSLSheet.xsl`.
7. Click OK and your list should render again with the rating field displayed
Enjoy!