SetRating in Reputation Class in SharePoint 2013 using Javascript and C# CSOM

Sathish Nadarajan
 
Solution Architect
November 16, 2013
 
Rate this article
 
Views
25901

Some time back, we saw how to do the SetRating using the SocialRatingManager class here. But it requires modifications on the List Permission Settings, and moreover it has a dependency of the Social Rating Synchronization timer Job. Now, let us see an alternative option for this. By using the Class Reputation on the namespace Microsoft.Office.Server.ReputationModel, we can achieve this. Let us see, how easy this is.

First let us see, how to use this class in C#. The biggest advantage of this class is, this is part of our CSOM functionality. Hence, there is no dependency on the Microsoft.SharePoint.dll.

Implementing SetRating by Reputation on C# CSOM.

try

{

// Get the Client Context

using (ClientContext ctx = new ClientContext("http://sathishserver:20000/sites/VHS/"))

{

//Get the Web Object

Web w = ctx.Web;

List l = w.Lists.GetByTitle("SocialRating");

ctx.Load(l, info => info.Id);

ctx.ExecuteQuery();

string ListID = l.Id.ToString();

int ItemID = 2;

int RatingValue = 4;

//Call the SetRating methohd

Microsoft.Office.Server.ReputationModel.Reputation.SetRating(ctx, ListID, ItemID, RatingValue);

ctx.ExecuteQuery();

}

}

catch (Exception ex)

{

}

 try
             {
 // Get the Client Context
                 using (ClientContext ctx = new ClientContext("http://sathishserver:20000/sites/VHS/"))
                 {
 //Get the Web Object
                     Web w = ctx.Web;
                     List l = w.Lists.GetByTitle("SocialRating");
                     ctx.Load(l, info => info.Id);
                     ctx.ExecuteQuery();
 
 
 
                     string ListID = l.Id.ToString();
                     int ItemID = 2;
                     int RatingValue = 4;
 //Call the SetRating methohd
                     Microsoft.Office.Server.ReputationModel.Reputation.SetRating(ctx, ListID, ItemID, RatingValue);
                     ctx.ExecuteQuery();
                 }
             }
             catch (Exception ex)
             {
  
             }
 

The code snippet is somewhat straight forward. Only thing, is the parameters for SetRating method. Even the method is not overloaded. Only 4 parameters are required.

Ctx – Client Context

ListID – String (GUID of the List)

Item ID – Int (List Item ID)

RatingValue – Int (Value ranging from 0 to 5)

 

Implementing SetRating by Reputation on Javascript.

Nowadays javascripts become more powerful. Through which we are able to call any method, access any objects from any dll. Let us see, how we can use Javascript for calling the SetRating method and how we are utilizing them.

 

 function SetRating() {
 
         itemID = 4;
 listID = “0F4BA645-5753-4526-A978-6BF2C0A2E654”
 RatingValue = 1;
 
         EnsureScriptFunc('reputation.js',
                 'Microsoft.Office.Server.ReputationModel.Reputation',
                 function () {
                     var ctx = new SP.ClientContext(“http://sathishserver:20000/sites/PublishingSite/”);
                     rating = Microsoft.Office.Server.ReputationModel.Reputation.setRating(ctx, listID, itemID, RatingValue);
 
                     ctx.executeQueryAsync(Function.createDelegate(this, this.RatingSuccess), Function.createDelegate(this, this.RatingFailure));
                 });
 
     }
 
 
     function RatingSuccess(sender, args) {
         alert('Rating Done Successfully');
     }
 
 
 
     function RatingFailure(sender, args) {
         alert(‘SetRating failed:' + args.get_message());
     }
 

But there is no other straight forward method to fetch the Rating. For that, we need to go with the normal list item itself. Let us see that how to fetch from Javascript.

 function GetRating(sender, args) {
 
                
         var ctx = new SP.ClientContext(“http://sathishserver:20000/sites/VHS/”);
         var web = ctx.get_web();
         this.list = web.get_lists().getByTitle(ListName);
         this.listItems = list.getItemsByID(ItemID);
 
         ctx.load(listItems, 'Include(ID, AverageRating)');
         ctx.executeQueryAsync(Function.createDelegate(this, GetRatingSuccess), Function.createDelegate(this, GetRatingFailure));
 
     }
 
 function GetRatingSuccess (sender, args) {
 
         RatingValue = listItems.itemAt(0).get_item("AverageRating");
         
 
     }
 
     function GetRatingFailure (sender, args) {
 
         alert(‘GetRating():' + args.get_message());
 
     }
 
 

 

In the same manner, by C# also, there is no direct method. We need to use the client context to fetch the item and the property “AverageRating” will give you the Rating Value.

That’s all. It is very simple when comparing with the SocialRating Manager.

 

Happy Coding..

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment