Enable Rating In SharePoint Document Library

Introduction

SharePoint 2013 comes with so many new features, one of which is to set the rating in the document library. This is very useful for document management and filtering documents by most ratings.

Solution:

SharePoint provides an easy out of the box way to enable this setting in Document Library. You can do it by going to Document Library setting, under general setting look for Rating setting. To enable rating check “Allow items in this list to be rated” to Yes. SharePoint also provide an option to choose Like/ Dislike option or star rating.

rating

Now we will see how to achieve the same thing programmatically.

The following code snippet enable Like/Rating in Document Library

  1. using system.Reflection;  
  2. using Microsoft.SharePoint;  
  3.   
  4. public static void EnableRating(SPList oLibrary)  
  5. {  
  6.     Assembly assembly = Assembly.Load("Microsoft.SharePoint.Portal, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");  
  7.     Type reputationHelper = assembly.GetType("Microsoft.SharePoint.Portal.ReputationHelper");  
  8.   
  9.     MethodInfodisableRatingMethod = reputationHelper.GetMethod("DisableReputation", BindingFlags.Static | BindingFlags.NonPublic);  
  10.     disableRatingMethod.Invoke(nullnew Object[]   
  11.     {  
  12.         oLibrary  
  13.     });  
  14.   
  15.     MethodInfoenableRatingMethod = reputationHelper.GetMethod("EnableReputation", BindingFlags.Static | BindingFlags.NonPublic);  
  16.     // "Likes" or "Ratings" as per your requirement.  
  17.     enableRatingMethod.Invoke(nullnew Object[]  
  18.     {  
  19.         oLibrary,  
  20.         "Ratings",  
  21.         false  
  22.     });  
  23.   
  24.     oLibrary.Update();  
  25.   
  26. }  
The above method loads the Microsoft.SharePoint.Portal assembly and gets the RaputationHelper class. RaputationHelper class has DisableReputation and EnableReputation method to toggle the Rating setting.

rating

Code Explanation

Assembly.Load method loads the dll dynamically in the application, it takes the four parameters, name of the dll you want to load, its version number culture and publiscKeyToken. reputationHelper is the class which is coming from Microsoft.SharePoint.Portal assembly. There are multiple ways to call a method in the class. GetMethod method is used for getting the methods dynamically. We are calling thee DisableReputation and EnableReputation methods to toggle the rating settings in the Document library.

BindingFlag.Static means the Disable Reputation method is a static method and it is non-public method.

Enable rating is executed by calling execute method. If the method has parameter, it has to be passed as object array. First is the Library object, and then the type of rating you want to enable either Like or Ratings. If you select Like then you get the vote option and if you select the rating, rating setting gets enabled. 

That’s all friends... let me know in the comments how you feel about this article.
 
Read more articles on SharePoint:

 

Up Next
    Ebook Download
    View all
    Learn
    View all