3

I created a library with images, there are many folders and into folder there are one or many images. I need get the likes(rating) of the image by current user.

Please help me to find the likedby in SharePoint 2016 rest api (Document Library).

        galleryService.getAll()
                .done(function (response) {

                    $scope.galleryimages = [];


                    var resultfolder = response.data.d.results;

                    angular.forEach(resultfolder, function (valuefolder, key) {
                        galleryService.getImagesByFolder(valuefolder.Title).done(function (response) {

                            angular.forEach(response.data.d.results, function (valueimage, key) {                                   


                                 ¿¿¿ LikedBy ????

                                var image = {
                                    Id: valueimage.ListItemAllFields.ID,
                                    Title: valuecarpeta.Title,
                                    FileRef: valuecarpeta.FileRef

                                };
                                $scope.galleryimages.push(image);

                            });
                        });
                    });
                });

////Query rest api

method: getImagesByFolder

var query = "/_api/web/GetFolderByServerRelativeUrl('galleryimages/" + folderName + "')/files?" + "$select=Name,ServerRelativeUrl&$top=1&$orderby=Name%20asc";

5

You could consider the following options

Option 1.

1) modify the query to include additional information. Since Ratings properties belong to ListItem resource, append the following expression: $expand=ListItemAllFields to the endpoint url

2) once the data is returned perform the filtering by on the client side by Ratings field

Example:

var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('/sites/contoso/documents')/files?$expand=ListItemAllFields";

$.getJSON(url)
.then(function(data){
     var result =  data.value.filter(function(item){
         if(item.ListItemAllFields.LikedByStringId.indexOf(_spPageContextInfo.userId.toString()) >= 0)
            return item;
     }); 

     console.log(result.length);
});

Option 2.

Replace the endpoint /_api/web/GetFolderByServerRelativeUrl('<url>')/files with GetItems method:

Url:/_api/web/Lists/GetByTitle('<listTitle>')/GetItems?$expand=File
Method: POST
Data:  {
  "query": {
    "__metadata": {
      "type": "SP.CamlQuery"
    },
    "ViewXml": "<View><Query><Where><Eq><FieldRef Name=\"LikedBy\" LookupId=\"TRUE\"/><Value Type=\"UserMulti\">17</Value></Eq></Where></Query></View>",
    "FolderServerRelativeUrl": "/sites/contoso/Documents/Archive"
  }
}

Headers:
     X-RequestDigest: <request digest>
     Accept: "application/json; odata=verbose"
     Content-Type : "application/json; odata=verbose"

Advantages:

  • filtering by Ratings field is perform on the server side

  • file along with associated list items could be retrieved via a single request

Example

var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/GetByTitle('Documents')/GetItems?$expand=File";
var query = "<View>" +
              "<Query>" +
                  "<Where>" +
                      "<Eq>" +
                         "<FieldRef Name=\"LikedBy\" LookupId=\"TRUE\"/>" +
                         "<Value Type=\"UserMulti\">" +
                              _spPageContextInfo.userId +
                         "</Value>" +
                       "</Eq>" +
                   "</Where>" +
               "</Query>" +
           "</View>";


var folderUrl = _spPageContextInfo.webServerRelativeUrl + "/Documents/Archive";

var queryPayload = { "query" :
           {
               "__metadata": { "type": "SP.CamlQuery" },
               "ViewXml": query,
               "FolderServerRelativeUrl": folderUrl 
           }
        };


executePost(url,queryPayload)
.then(function(data){
    var items = data.d.results;
    items.forEach(function(item){
       //console.log(item.LikesCount); //Likes count
       //console.log(item.LikedByStringId); //user ids
       console.log(item.File.Name); //File properties like Name    
    });

})

where

function executePost(url,payload){
    return $.ajax({
        url: url,
        method: "POST",
        data: JSON.stringify(queryPayload),
        headers: {
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "Accept": "application/json; odata=verbose",
            "Content-Type": "application/json; odata=verbose"
        }
    });
}    
  • 1
    thank you so much, for this solution. I used the solution Option 2. It worked for me – Yojan Morales Oct 11 '17 at 13:03
  • I'm trying option 2 and I am able to filter on the LikedBy and I get the LikesCount, but neither the LikedBy nor the LikedByStringId are included in my results. Any ideas? – Rothrock Nov 21 '17 at 0:14
  • So I tried option 1 and again I get the ListItemAllFields, but don't get LikeBy or LikedByStringId. – Rothrock Nov 21 '17 at 0:30
0

Please use the following line of code to get the item information(include LikedBy).

var query = "/_api/web/lists/GetByTitle('galleryimages')/Items?$filter=substringof('/"+folderName+"/',FileRef)&$select=ID,FileLeafRef,Title,FileRef,LikesCount,LikedBy/Title&$top=1&$orderby=FileLeafRef asc&$expand=LikedBy";
  • thank you for you answer me, but the query doesn't return anything. :( filter=substringof('/"+folderName+"/',FileRef) not working, if I remove this one (filter) return records fine. – Yojan Morales Sep 27 '17 at 14:22
  • Check whether the value of folderName variable is right or not. – LZ_MSFT Sep 28 '17 at 1:16
  • the value is correct, I changed way to consume data, I maked by CSOM. Thank you – Yojan Morales Sep 28 '17 at 15:38
  • Yes, we can use JSOM to achieve it. – LZ_MSFT Sep 29 '17 at 1:25

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.