5

iam tring following query but getting error on "LikesCount". i have Likes column in list . but after executing following query iam getting error on Likes count . Error is "The field LikesCount" of type "Likes" cannot be used in the query sort expression.

var uri= appweburl + "/_api/SP.AppContextSite(@target)/web/Lists/GetByTitle('listName')/items?$select=LikedBy/Title,LikedBy/ID,Title,Id,LikesCount&$expand=LikedBy&$filter=" + selectedFilter + " +'&$orderby=LikesCount desc&@target='" + hostweburl + "'";
  • What kind of field is LikesCount? – Pantani Feb 10 '16 at 11:17
6

It's still weird that $orderby does not work with Likes and Calculated Type columns. So now can do following things.

Sort them in client side

Ignore $orderby from url

var uri= appweburl + "/_api/SP.AppContextSite(@target)/web/Lists/GetByTitle('listName')/items?$select=LikedBy/Title,LikedBy/ID,Title,Id,LikesCount&$expand=LikedBy&$filter=" + selectedFilter + " +'&@target='" + hostweburl + "'";

After getting the response, you can sort using array.sort()

var sortedResult = response.d.results.sort(function(a, b) {
    return b.LikesCount - a.LikesCount;
});

Or using any third party js library. You can use underscore-min.js

var sortedResult = _.chain(response.d.results)
                        .sortBy('LikesCount')
                        .reverse()
                        .partition('LikesCount')
                        .flatten()
                        .value();

When to use underscore.js, find it in the official doc.

Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It’s the answer to the question: “If I sit down in front of a blank HTML page, and want to start being productive immediately, what do I need?” … and the tie to go along with jQuery's tux and Backbone's suspenders.

Underscore provides over 100 functions that support both your favorite workaday functional helpers: map, filter, invoke — as well as more specialized goodies: function binding, javascript templating, creating quick indexes, deep equality testing, and so on.

Using CAML

You can use CAML to do this. In that case, you have to send POST request.

CAML query should look like following:

"<View> " + 
 "  <Query> " + 
 "    <OrderBy> " + 
 "      <FieldRef Name=\"LikesCount\" Ascending=\"FALSE\" /> " + 
 "    </OrderBy> " + 
 "    <Where> " + 
 "      <Eq> " + 
 "        <FieldRef Name=\"{FieldName}\" /> " + 
 "        <Value Type=\"Text\">{Value}</Value> " + 
 "      </Eq> " + 
 "    </Where> " + 
 "  </Query> " + 
 "</View> "

Request body:

var data = {
    query: {
        __metadata: {
            'type': 'SP.CamlQuery'
        },
        ViewXml: query
    }
};

Request URL

/_api/SP.AppContextSite(@target)/web/Lists/GetByTitle('listName')/getitems?$select=LikedBy/Title,LikedBy/ID,Title,Id,LikesCount&$expand=LikedBy
  • I wanted to +1 but you went straight for a dependency just to sort. – iOnline247 Feb 10 '16 at 12:09
  • It can be done manually. I mean using pure js. That I suggested also! – Atish Dipongkor - MVP Feb 10 '16 at 12:12
  • I read that as well. The example isn't laid out that way though. :P – iOnline247 Feb 10 '16 at 14:59
  • I like underscore.js for data manipulation than writing by own. It has several utility methods. You can try as well. – Atish Dipongkor - MVP Feb 10 '16 at 15:05
  • I like underscore but I love lodash, for obvious reasons. It'd be nice to see your answer w/o another dependency needed/implied. That's all. – iOnline247 Feb 10 '16 at 15:41

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.