Wednesday, March 2, 2016

How to send a “like” or “unlike” command via REST API?

<script type="text/javascript">

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);
});

function UpdateLike() {
    var itemId = 1;
    var listId = "361d6bc5-0034-4f53-8f7b-f7198a3a844c";//set list id
    var setLike = true;//set false to unlike

    var ctx = new SP.ClientContext("siteUrl");//your site url
    Microsoft.Office.Server.ReputationModel.Reputation.setLike(ctx, listId, itemId, setLike);

    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());//note that you will get error if try like twice
}
</script>

No comments:

Post a Comment