4

I have created a custom list and activated rating setting.

I want to count the number of likes and likedby. But when I modify the list view I found Likedby is disable but checked and likedby is not listed in

siteurl /_vti_bin/ListData.svc/SampleList()

or

siteurl/_api/web/lists/getByTitle('SampleList')/items()

Please help me to find the likedby in SharePoint Online.

1

2 Answers 2

3

LikedBy is a multi valued lookup column. You have to $expand it in the query. Just make GET request to the following end-point, in response you will get LikesCount and LikedBy

/_api/web/lists/GetByTitle('List Name')/Items?$select=LikesCount,LikedBy/Title&$expand=LikedBy

Above query will give you the Title of LikedBy user as I am using LikedBy/Title in $select like following

    {
  "d": {
    "results": [
      {
        "__metadata": {
          "id": "f2592d31-7960-42c1-a51b-b0cbf6238483",
          "uri": "https://xxx.sharepoint.com/_api/Web/Lists(guid'ba8a6814-61cd-4dc6-92f2-ae088316744a')/Items(1)",
          "etag": "\"2\"",
          "type": "SP.Data.Short_x0020_DescriptionListItem"
        },
        "LikedBy": {
          "results": [
            {
              "__metadata": {
                "id": "e81c95c7-b0f0-44ff-aa46-71d6a7782bc8",
                "type": "SP.Data.UserInfoItem"
              },
              "Title": "Mohammad Mazumder"
            }
          ]
        },
        "LikesCount": 1
      }
    ]
  }
}

If you need more fields of LikedBy user, then mention them in the $select like following.

$select=LikesCount,LikedBy/Id,LikedBy/Title&$expand=LikedBy

If you have time, have a look on this article.

0

I did something quite similar in SharePoint 2013 on-prem. You can check my step-by-step guide here. Maybe there are some useful information.

Also here a code snippet I used:

var listId = 'E7585AC3-82C5-4D02-A9E9-58A735DB89B9';
var siteUrl = '/';
var itemId = 1;

    function GetLikeCount(j) {

        //alert("Run for Item ID: " + j);  
        var passedItemId = j;
        var context = SP.ClientContext.get_current();
        var list = context.get_web().get_lists().getById(listId);
        var item = list.getItemById(passedItemId);

        context.load(item, "LikedBy", "ID", "LikesCount");
        context.executeQueryAsync(Function.createDelegate(this, function (success) {

            //Check if the user id of the current users is in the collection LikedBy. 
            var likeDisplay = true;
            var $v_0 = item.get_item('LikedBy');
            var itemc = item.get_item('LikesCount');

            //alert(itemc);
            if (!SP.ScriptHelpers.isNullOrUndefined($v_0)) {
                for (var $v_1 = 0, $v_2 = $v_0.length; $v_1 < $v_2; $v_1++){
                    var $v_3 = $v_0[$v_1];
                    if ($v_3.$1E_1 === _spPageContextInfo.userId) {
                        //cb(true, item.get_item('LikesCount'));

                        //alert("Liked by me");
                        likeDisplay = false;
                    }
                }
            }
            ChangeLikeText(likeDisplay, itemc, passedItemId);

        }), Function.createDelegate(this, function (sender, args) {
            //alert('F1');
        }));

    }

If you have some questions, don't be afraIT to ask.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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