1

I am trying to update the LikedBy column in Discussion Board. LikedBy, it seems, is a lookup column. Can you let me know the what the payload would be? I will need to update the Likedby Id and Likedby Title. I am able to update the likescount, but not the Likedby Id and LikedBy Title.

I am sort of stuck on this.

1
  • What does the lookup field point to? – wjervis Dec 21 '16 at 18:27
1

If you are referring to Ratings fields, then here are use cases that shows on how to set proper payload:

1) clear all likes

var itemProperties = {
    '__metadata': { "type": "SP.Data.DiscussionsListItem" },
    "LikedByStringId": { "results" :  [] },
    "LikesCount" : 0
};

2)set like by user with id = 1

var itemProperties = {
    '__metadata': { "type": "SP.Data.DiscussionsListItem" },
    "LikedByStringId": { "results" :  ["1"] },
    "LikesCount" : 1
};

Note: LikesCount property should be provided in conjunction with LikedByStringId

Example

var listTitle = "Discussions";
var itemProperties = {
    '__metadata': { "type": "SP.Data.DiscussionsListItem" },
    "LikedByStringId": { "results" :  ["1"] },
    LikesCount : 1
};
var itemId = 1;
var webUrl = _spPageContextInfo.webServerRelativeUrl;


updateListItem(webUrl,listTitle,itemId,itemProperties)
.done(function(item)
{
    console.log('List item has been updated');
})
.fail(function(error){
    console.log(JSON.stringify(error));
});

where

function executeJson(options) 
{
    var headers = options.headers || {};
    headers["Accept"] = "application/json;odata=verbose";
    if(options.method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }   

    var ajaxOptions = 
    {       
       url: options.url,   
       type: options.method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if(options.method == "POST") {
      ajaxOptions.data = JSON.stringify(options.payload);
    }  

    return $.ajax(ajaxOptions);
}


function updateListItem(webUrl,listTitle,itemId,itemPayload)
{
     var options ={
        url: webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + itemId + ")",  
        method: "POST",
        headers : {
          "X-HTTP-Method": "MERGE",
          "If-Match": "*"
        },
        payload: itemPayload    
     };     
     return executeJson(options);
}
3
  • Thanks. I was able to update the Likedby.. with this.. But I don't want to clear the existing likes.. I want my userid to(for example ) to be added to the already existing userid collection. I was trying this.. by first getting the existing userid's and then adding the new userid.. but I have issues with this approach. Is there any other way.. by which I can append the new userid to existing user Id's collection. – Sony Dec 26 '16 at 12:00
  • @Sony, follow setLike.js, it demonstrates a generic way (it simulates Microsoft.Office.Server.ReputationModel.Reputation.setLike method to some extent) to like/unlike a resource – Vadim Gremyachev Dec 26 '16 at 14:35
  • @Sony, if you find this answer helpful, you could also consider to accept it – Vadim Gremyachev Dec 26 '16 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.