1

I've created a blog on Sharepoint 2010 and want to query the list via REST for reporting. I want to order the list by the default field Category (internal name PostCategory). Unfortunately, this is a multiselect field, therefore a simple "?$orderby=Category" doesn't work. I've also tried to expand the Category, but that doesn't work either.

Is there a chance, that I can order the list using rest? What about more then one selected Category? Can it be ordered by the first category, then the second, etc.?

If it's not possible using REST, what about ordering within JSON? I use a small javascript, that puts the list in a reporting format. Can I order within the JSON result?

Here is an example:

// Create REST-API URL
var strURL = "<REST-URL>";

// Get information from REST-API and create html output
$.getJSON(strURL, function(data) {
        <Create output>
    };

// Append to webpart
$('#<WebPartTitle>').append($(html));

I've asked this question already here, but thought, that this is the place more likely to get an answer ;-)

1

The following example demonstrates how to order Posts by Category field.

But there is one important note here:

Since Category field is a multiple choice field value, it is assumed that only one category could be specified per post.

For that purpose FirstCategoryTitle property is introduced which represent the title of first category in post item.

Example

var endpointUrl = 'http://contoso.intranet.com/blog/_vti_bin/listdata.svc/Posts?$expand=Category';

$.getJSON(endpointUrl, function(data) {
   var items  = data.d.results.map(function(item){
       item.FirstCategoryTitle = (item.Category.results.length > 0 ? item.Category.results[0].Title : '');   //get first category
       return item;
   });
   items.sort(postComparer);   //sort by category
   items.forEach(function(item){
     console.log(item.Title);    
   });

});

function postComparer(x,y) {
  return x.FirstCategoryTitle > y.FirstCategoryTitle;
}

Update

Without map Method:

$.getJSON(endpointUrl, function(data) {
   var items  = data.d.results;
   items.sort(postComparer);   //sort by category
   items.forEach(function(item){
     console.log(item.Title);    
   });

});

function postComparer(x,y) {
   return getFirstCategoryTitle(x) > getFirstCategoryTitle(y);
}

function getFirstCategoryTitle(item)
{
   return (item.Category.results.length > 0 ? item.Category.results[0].Title : '');   
}
  • Nice, that did the trick! I had to play with the language discrepancies, but after that it was working immediately. Since I'm not so much into javascript, I don't get, how exactly it is working, espacially the function postComparer. But it shows, how powerful javascript is and I try to figure that out as well :-) – Alez May 21 '15 at 11:24
  • 1
    Oh crap, I've tested the whole day using Firefox and everything looked fine, but all the users have IE11 and there I get the error: "The object doesn't support the property or method "map"". IE is already running in Enterprise mode and the Sharepoint requests IE8 and I've tried also to force it on the client, but still get the error. Any ideas, how this can be solved? – Alez May 21 '15 at 14:46
  • Sure, the answer has been updated – Vadim Gremyachev May 21 '15 at 14:56
  • Wow, you are fast! Now it works on IE11 as well. Thanks a lot (and I'm feeling dumb because of the simplicity of the answer....) – Alez May 21 '15 at 15:44
  • This morning I've realized, that this freaking IE is still not sorting correctly (I was obiously to happy, that it at least shows something...). I had to make a small change to your script, line items.forEach(function(item){ needs to be $.each(items, function(i,item) { because forEach is also not support. The entries however are still ordered by the ID instead of the updated ordering... Any ideas on that? I've also tried to disable the enterprise mode and compatibility mode of IE but here's the same. – Alez May 22 '15 at 8:53
1

Another answer, since the previous code provided by Vadim is working with firefox, but makes problems with IE.

The function postComparer must be like this (not using the MAP method):

function postComparer(x,y) {
return getFirstCategoryTitle(x) > getFirstCategoryTitle(y) ? 1 : getFirstCategoryTitle(x) < getFirstCategoryTitle(y) ? -1 : 0;
};

The problem is, that the sort function postComparer as provided by Vadim is returning boolean values, whereas the correct behaviour of sort would be to return either negative or positive values or 0, if equal. More details can be found here and here.

  • +1 Indeed, the proper way would be to return int – Vadim Gremyachev Jun 4 '15 at 21:05

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.