1

I am trying to use SP Services to get a filtered and sorted list of data. My service call looks like this.

var query = "<Query>" +
                "<Where>" +
                    "<Eq>" +
                        "<FieldRef Name='Status'/><Value Type='Text'>Active</Value>" +
                    "</Eq>" +
                "</Where>" +
                "<OrderBy>" +
                    "<FieldRef Name='Order' />" +
                "</OrderBy>" +
            "</Query>";

var viewFields = "<ViewFields>" +
                    "<FieldRef Name='Title' />" +
                    "<FieldRef Name='backgroundImage' />" +
                    "<FieldRef Name='link' />" +
                "</ViewFields>";

$().SPServices({
    operation: "GetListItems",
    webURL: "a/url/",
    async: true,
    listName: "Slider",
    CAMLQuery: query,
    CAMLViewFields: viewFields,
    completefunc: function (xData, Status) {
        var slides = [];
        $(xData.responseXML).SPFilterNode("z:row").each(function() {
            slides.push({
                title: $(this).attr("ows_Title"),
                bgImage: $(this).attr("ows_backgroundImage"),
                link: $(this).attr("ows_link"),
            });
        });
        callback(slides);
    }
});

The problem I am having is that the where clause is being applied, but the order by is not. I have tried wrapping the node in a node as suggested in this post: Caml Query for Order By List for web service call not ordering . This gave me an internal server error.

I have also tried removing the Query tag entirely. Can anyone see why the order by clause is not being applied? This gave me a bad request error.

The Order field is a number typed column.

EDIT (solution):

My problem was that I named my list column 'Order' which apparently is implicitly created. Sharepoint renamed my column 'Order0' without me knowing. I ended up deleting my original column and creating a new one called 'slideOrder'

2
  • Can you add an Ascending='True' into "<FieldRef Name='Order' />", for example "<FieldRef Name='Order' Ascending='True'/>", and see if that works? Nov 2, 2016 at 16:33
  • Sadly that did not work. Thanks for the attempt though. Nov 2, 2016 at 16:35

1 Answer 1

0

I don't have a direct answer but here are some steps you can do to troubleshoot:

  • Try removing the View Fields from the SPServices Call
  • Try adding a rowlimit like this:

    CAMLRowLimit: 1, 
    
  • Try OrderBy on another field such as Title to see if that works

  • Try the query with just the OrderBy element
  • Ensure 'Order' is the Internal name of your field and is type of Number or Text
  • Consider using REST API instead of SPServices
2
  • Bullet number 5 was the correct answer. Apparently the name 'Order' was already taken so it implicitly renamed my 'Order0'. Deleted the column and made a new one called 'slideOrder'. Nov 2, 2016 at 17:27
  • yeah sounded like there was a conflict there. Glad you got it Nov 2, 2016 at 19:51

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.