SharePoint Pals
 | Sign In
SetRating in Reputation Class in SharePoint 2013 using Javascript and C# CSOM
by Sathish Nadarajan 16 Nov 2013
Author
SharePoint MVP
Visits   
Today  :   10     Total  :    19287

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.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation