How to enable and disable SharePoint 2010 rating programmatically


There might be the case that If you have provisioned all the sites hierarchies very fine in the production environment and now request is to enable the ratings on all the lists and libraries in each site and subsites. Of course not manually right? So How to do that?

Yes you are right, you can write a feature, or a console application or any other approach which will enable rating setting recursively on each list and library within the site.

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

typeof(Microsoft.SharePoint.Portal.RatingsSettingsPage).

GetMethod("EnableRatings", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic).

Invoke(nullnew 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.

object of SPList on which you need to enable the ratings

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(nullnew object[] { _objectOfSPList });


This method accepts a single parameter which is as the SPList and this is the target list.