Create Rating Visual Web Part Using Visual Studio 2010,

Create Rating Visual Web Part Using Visual Studio 2010,  also Apply Rating Settings using c#

  Back End Design
 Register Assembly
 
 <%@ Register Tagprefix="SharePointPortalControls" Namespace="Microsoft.SharePoint.Portal.WebControls" Assembly="Microsoft.SharePoint.Portal, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>



Add Control.

<div>    Average Ratting :
   <SharePointPortalControls:AverageRatingFieldControl ID="PageRatingControl" runat="server"/>
</div>




C# Code  :
required Assembly

using Microsoft.SharePoint.WebControls;
using System.Reflection;
using Microsoft.SharePoint.Portal.WebControls;
using Microsoft.SharePoint;
 private readonly static Guid averageRatingId = new Guid("5a14d1ab-1513-48c7-97b3-657a5ba6c742");
        private readonly static Guid ratingCountId = new Guid("b1996002-9167-45e5-a4df-b2c41c6723c7");

        protected void Page_Load(object sender, EventArgs e)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
              {
                  SPList _reportList = SPContext.Current.Web.Lists.TryGetList("Pages");
                  PageRatingControl.ListId = _reportList.ID;
                  PageRatingControl.ControlMode = SPControlMode.Edit;
                  PageRatingControl.FieldName = "AverageRating";
                  ActivateRatingForList(_reportList);
              });
        }
// Apply Rating Settings For List using Code
        public static void ActivateRatingForList(SPList _list)
        {
            SPWeb _Web = SPContext.Current.Web;
            _Web.AllowUnsafeUpdates = true;

            SPField _averageRating = _list.ParentWeb.AvailableFields[averageRatingId];
            if (_averageRating != null && !_list.Fields.Contains(_averageRating.Id))
            {
                _list.Fields.Add(_averageRating);
            }
            SPField _ratingCount = _list.ParentWeb.AvailableFields[ratingCountId];
            if (_ratingCount != null && !_list.Fields.Contains(_ratingCount.Id))
            {
                _list.Fields.Add(_ratingCount);
            }
            _list.Update();
        }

Popular posts from this blog

Register Event Handlers plus free Site Settings – Manage Event Handlers Add-on solution

Custom Form Base Login Web Part With Sharepoint 2010

Real World Branding with SharePoint 2010 Publishing Sites