1. Add Rating on a Layout Page:-
--------------------------------------------
1. Open any layout page add below ‘Rate this article” div code
<asp:content contentplaceholderid=" PlaceHolderMain" runat="server">
<div class="welcome blank-wp">
<div>
Rate this article:
<SharePointPortalControls:Aver ageRatingFieldControl
ID="PageRatingControl"
FieldName="AverageRating"
runat="server"/>
</div>
2. Enable rating feature for a pages list form list settings.
3. Open a page which using page layout having rating field control
4. Give the rating on page and go to list page and check. Total no.of ratings is increased.
----------------------------------------------------------------------------------------------------------------------------------------------------
2. Rating WebPart in SharePoint:-
--------------------------------------------------
RatingVisualWebPart.ascx:-
--------------------------
<%@ Assembly Name="$SharePoint.Project. AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken= 71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft. SharePoint.WebControls"Assembly="Microsoft. SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken= 71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft. SharePoint.Utilities"Assembly="Microsoft. SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken= 71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web. Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken= 31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft. SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft. SharePoint.WebPartPages"Assembly="Microsoft. SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken= 71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind=" RatingVisualWebPart.ascx.cs" Inherits="Rating. RatingVisualWebPart. RatingVisualWebPart"%>
<%@ Register TagPrefix=" SharePointPortalControls"
Namespace="Microsoft. SharePoint.Portal.WebControls"
Assembly="Microsoft. SharePoint.Portal, Version=14.0.0.0,Culture= neutral,PublicKeyToken= 71e9bce111e9429c" %>
<%--<span class="content_small_text">
<SharePointPortalControls: averageratingfieldcontrol id="rating" runat="server" controlmode="Edit"/>
</span>--%>
<span class="content_small_text">
<asp:Panel ID="pnl1" runat="server">
</asp:Panel>
</span>
RatingVisualWebPart.ascx.cs:-
----------------------------------------------
using System;
using System.ComponentModel;
using System.Web.UI.WebControls. WebParts;
using Microsoft.SharePoint.Portal;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Portal. WebControls;
using Microsoft.SharePoint. WebControls;
using System.Web;
namespace Rating.RatingVisualWebPart
{
[ToolboxItemAttribute(false)]
public partial class RatingVisualWebPart : WebPart
{
public RatingVisualWebPart()
{ }
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeControl();
}
protected void Page_Load(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
SPList testList = SPContext.Current.Web.Lists. TryGetList("Pages");
SPListItem testItem = testList.GetItemById(249);
AverageRatingFieldControl ratingCntrl = new AverageRatingFieldControl();
ratingCntrl.ID = "RatingCntrl";
ratingCntrl.ListId = testList.ID;
ratingCntrl.ControlMode = SPControlMode.Edit;
ratingCntrl.FieldName = "AverageRating";
ratingCntrl.ItemContext = SPContext.GetContext(context, testItem.ID, testList.ID, SPContext.Current.Web);
pnl1.Controls.Add(ratingCntrl) ;
AverageRatingFieldControl arfcRating = new AverageRatingFieldControl();
arfcRating.ID = "rating";
arfcRating.ListId = testList.ID;
arfcRating.FieldName = "Rating (0-5)";
arfcRating.ControlMode = SPControlMode.Edit;
arfcRating.ItemContext = SPContext.GetContext(context, testItem.ID,testItem.ParentList.ID, testItem.ListItems.List. ParentWeb);
pnl1.Controls.Add(arfcRating);
}
}
}
Hi Sreekanth,
ReplyDeleteThanks for the useful post. I'm having a problem though. The list in my case is present in the parent web not the current Web. So, I'm passing the reference of the parent Web while creating the ItemContext object.The result is that I'm able to load the rating in the Control but when I try to change the rating, the rating is not updated (even after I run the 2 Social timer jobs). I investigated this problem by looking in to the Social Database and there I found the problem. The Rating entry was there in the Urls table but the Url itself was wrong. It was something like http://devportal/subsite/_layouts/listform.aspx?PageType=4&ListId=%7BFE6492C8-EDE2-4B92-B528-83CF0F1BBACC%7D&ID=3 whereas It should have been http://devportal/_layouts/listform.aspx?PageType=4&ListId=%7BFE6492C8-EDE2-4B92-B528-83CF0F1BBACC%7D&ID=3. Seems like the AverageRatingControl only works if the List is present on the current web. Any comments?
Faisal
Hi Sreekanth:
ReplyDeleteI'm using this code in a SharePoint 2013 webpart, but I'm having a problem with the average Rating format.
If I look into the script that is injected with this code I found the following:
EnsureScript('SP.UI.Ratings.js', typeof(SP.UI.Reputation), function () {
var avgRating = 4,5;var ratingCount = '0';var ratings = '';
... continue
If you can see, the avgRating variable is filled with a '4,5' instead of a '4.5' so the script is throwing an exception "Unexpected number". If the average rating is an exact integer, the control works just fine. Is it possible to force a specific culture in your code? Or there is any other way to get around this issue? We are using the Spanish Language Pack.
Regards,
Gustavo