Like/Unlike SharePoint List Items from SharePoint Hosted App

To enable rating refer this article – https://support.office.com/en-us/article/Add-a-ratings-feature-to-your-library-5901fcfd-19ca-4f27-a65f-284654298552

Code to update like:

 

 function setLike(likeFlag)
 {
 SP.SOD.registerSod('reputation.js', '/_layouts/15/reputation.js');
 SP.SOD.executeFunc('reputation.js', 'Microsoft.Office.Server.ReputationModel.Reputation', function () {
 SP.SOD.executeFunc('sp.js', 'SP.ClientContext', UpdateLike(likeFlag));
 });
 }

 function UpdateLike(likeFlag) {
 var listId = "6d9e46c2-b396-4a4f-bc1f-6e265dfb6fe6"; //set list id and make sure rating is enabled in the list 
 var itemId = 3; //List Item Id 
 var ctx = new SP.ClientContext.get_current();
 Microsoft.Office.Server.ReputationModel.Reputation.setLike(ctx, listId, itemId, likeFlag); 
 ctx.executeQueryAsync(Function.createDelegate(this, this.RatingSuccess), Function.createDelegate(this, this.RatingFailure));
 }; 

 function RatingSuccess(sender, args) {
 alert('Rating updated successfully');
 } 

 function RatingFailure(sender, args) {
 alert('Failed:' + args.get_message()); 
 }

 
<button onclick="setLike(true)">Like</button>
<button onclick="setLike(false)">Unlike</button>
Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

Comments are closed.

%d bloggers like this: