Pages

Friday, August 26, 2011

Working with SharePoint Ratings – Part 2

How to enable and disable SharePoint 2010 rating programmatically

If you have referred the details in the previous post on Working with the SharePoint 2010 Rating – Part 1 for theoretical explanation on what happens in the background, now let’s do something interesting in Visual Studio.
When you visit the list settings page and click on rating settings link , you gets redirected the application page called as “RatingSettings.aspx” with the query string as the list GUID and on this page you can decide the setting about enabling and disabling ratings on that list.
I didn’t see any other way to achieve this with programmatic approach in the server object model in SPList class and so for curiosity I opened up the reflector to see what Microsoft has done.
And so here are the ways to programmatically enable the ratings on list
1.       Using the reflection
typeof(Microsoft.SharePoint.Portal.RatingsSettingsPage).
GetMethod("EnableRatings", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).
Invoke(null, new object[] { _objectofSPList, true });

There is a method “EnableRatings” in class “RatingSettingsPage” and method is internal and static. So I am just invoking the method here. This method takes two parameters.
a.    object of SPList on which you need to enable the ratings
b.    a Boolean value (true/false) – keep true to prorogating rating on list items
There is one more method of this class, which allows us to disable the rating
typeof(Microsoft.SharePoint.Portal.RatingsSettingsPage).
GetMethod("DisableRatings", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).
Invoke(null, new object[] { _objectOfSPList });

This method accepts a single parameter which is as the SPList and this is the target list.
2. Using the actual code
      Rating can also be enabled without using the reflection, but in this case you write much more code than in previous method.
This method is used for enabling the rating settings on a list. You can see that there are two fields which are being add in the list.
  private void EnableRatingSetting(SPList _list)
  {
    SPFieldCollection _allfields = _list.Fields;
    SPFieldCollection _availFields = _list.ParentWeb.AvailableFields;
    if (!_allfields.Contains(FieldId.AverageRatings))
    {
       SPField field = _availFields[FieldId.AverageRatings];
_list.Fields.AddFieldAsXml(field.SchemaXmlWithResourceTokens, true, SPAddFieldOptions.AddFieldToDefaultView | SPAddFieldOptions.AddFieldInternalNameHint | SPAddFieldOptions.AddToAllContentTypes);
    }
    if (!_allfields.Contains(FieldId.RatingsCount) && _availFields.Contains(FieldId.RatingsCount))
    {
      SPField field2 = _availFields[FieldId.RatingsCount];
      _list.Fields.AddFieldAsXml(field2.SchemaXmlWithResourceTokens, false, SPAddFieldOptions.AddFieldInternalNameHint | SPAddFieldOptions.AddToAllContentTypes);
    }
      _list.Update();

   }

And this is the method to propagate the changes to the list items.
There is a method available with the SocialRatingManager class called as PropogateRating which takes care of this.
private void PropogateChanges(SPList _list)
{
SocialRatingManager _socialRatingMgr = new   SocialRatingManager(SPServiceContext.Current);
   string _baseUrl = _list.ParentWeb.Url;
   if (_baseUrl.EndsWith("/", StringComparison.OrdinalIgnoreCase))
   {
      _baseUrl = _baseUrl.TrimEnd(new char[] { '/' });
   }
   foreach (SPListItem item in _list.Items)
   {
     string _itemUrl = item.Url;
     if (_itemUrl.StartsWith("/", StringComparison.OrdinalIgnoreCase))
     {
        _itemUrl = _itemUrl.TrimStart(new char[] { '/' });
     }
     SPSecurity.RunWithElevatedPrivileges(delegate()
     {
        _socialRatingMgr.PropagateRating(new Uri(_baseUrl + "/" + _itemUrl));
     });
    }

   }

   private static SPField GetField(Guid id, SPFieldCollection fieldColl)
   {
      return fieldColl[id];
   }

This method is used to disable the rating on a list
private void DisableRatingSetting(SPList _list)
    {
       SPField _field1 = GetField(FieldId.AverageRatings, _list.Fields);
       if (_field1 != null)
       {
         _list.Fields.Delete(_field1.InternalName);
       }
       SPField _field2 = GetField(FieldId.RatingsCount, _list.Fields);
       if (_field2 != null)
       {
          _list.Fields.Delete(_field2.InternalName);
       }
        _list.Update();

    }

4 comments:

  1. What is the FieldId? thanks

    ReplyDelete
  2. FieldId is built in class available by Publishing APIs
    more reference here - http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.fieldid.aspx

    ReplyDelete
  3. Hi Bhushan,
    In SharePoint 2013, this has been slightly changed, as more columns are added to provide a richer rating experience:

    Logic:

    Collection ratingFieldIds = new Collection() { Constants.Fields.AverageRating.Id, Constants.Fields.RatingCount.Id, Constants.Fields.RatedBy.Id, Constants.Fields.Ratings.Id, Constants.Fields.LikesCount.Id, Constants.Fields.LikedBy.Id };

    foreach (Guid fieldId in ratingFieldIds)
    {
    SPField field = list.ParentWeb.AvailableFields[fieldId];
    if (field != null && !list.Fields.Contains(field.Id))
    {
    list.Fields.Add(field);
    }
    }

    list.RootFolder.Properties[Constants.Configuration.RatingsVotingExperience] = Constants.Configuration.Ratings;
    list.RootFolder.Update();

    list.Update();

    Constants:

    internal static readonly Field AverageRating = new Field() { Id = new Guid(“5a14d1ab-1513-48c7-97b3-657a5ba6c742″) };
    internal static readonly Field RatingCount = new Field() { Id = new Guid(“b1996002-9167-45e5-a4df-b2c41c6723c7″) };
    internal static readonly Field RatedBy = new Field() { Id = new Guid(“4D64B067-08C3-43DC-A87B-8B8E01673313″) };
    internal static readonly Field Ratings = new Field() { Id = new Guid(“434F51FB-FFD2-4A0E-A03B-CA3131AC67BA”) };
    internal static readonly Field LikesCount = new Field() { Id = new Guid(“6E4D832B-F610-41a8-B3E0-239608EFDA41″) };
    internal static readonly Field LikedBy = new Field() { Id = new Guid(“2CDCD5EB-846D-4f4d-9AAF-73E8E73C7312″) };

    internal static readonly string RatingsVotingExperience= “Ratings_VotingExperience”;
    internal static readonly string Ratings = “Ratings”;

    ReplyDelete
  4. Awesome , Thanks Ricardo for providing update on this.

    ReplyDelete