1

I have a list view set up via the webinterface of Sharepoint. Now I want to use this view (the CAML XML) to be loaded into javscript to provide list items for a Content Editor Webpart. The view works as intended on the Sharepoint website. But when I load the view via JavaScript my defined filters:

  1. Sort in descending order after Date of Creation
  2. Limit the displayed list items to 3 Items

don't work. The items returned from the AsyncQuery are all list items and are not ordered as specified.

Can anybody help me?

My Code:

getItemsFromView: function (listTitle, viewTitle, context)
    {
        var list = context.get_web().get_lists().getByTitle(listTitle);
        var view = list.get_views().getByTitle(viewTitle);
        var query = new SP.CamlQuery();
        context.load(view);
        context.load(list);
        context.executeQueryAsync(
            function(sender,args) { query.set_viewXml("<View><Query>" + view.get_viewQuery() + "</Query><RowLimit>3</RowLimit></View>");},
        function(sender,args) {alert('Getting news failed. ' + args.get_message() + '\n' + args.get_stackTrace())}
        );


        var items = list.getItems(query);
        context.load(items);

        context.executeQueryAsync(Function.createDelegate(this, function(){this.onQuerySucceeded(items)}), Function.createDelegate(this, this.onQueryFailed));
    },

1 Answer 1

1

Remove this:

context.load(view);
context.load(list);
context.executeQueryAsync(
function(sender,args) { query.set_viewXml("<View><Query>" + view.get_viewQuery() + "</Query><RowLimit>3</RowLimit></View>");},
function(sender,args) {alert('Getting news failed. ' + args.get_message() + '\n' + args.get_stackTrace())});

and replace with:

var myQuery = "YOUR CAML QUERY";
query.set_viewXml("<View><Query>" + myQuery + "</Query><RowLimit>3</RowLimit></View>");

If you really want to first call SharePoint to ask what the CAML query is formed, you need to chain your calls.

context.executeQueryAsync( ()=> { 
// Here you have received the view from the list and can access the query property view.get_viewQuery()
                                  query.set_viewXml("<View><Query>" + 
view.get_viewQuery() + "</Query><RowLimit>3</RowLimit></View>");
var items = list.getItems(query);
context.load(items);
                                  context.executeQueryAsync( 
                                     ()=> 
                                     { // Get result here }
                                     , 
                                     ()={ //second call failed })
                                }, 
                            ()=> { //first fail });

The chaining above is written in short version with TypeScript style. You need to rewrite with Function.delegate if you write pure javascript.

3
  • The solution with the hard coded query works fine.
    – maxkra
    Commented Dec 19, 2017 at 10:11
  • With the chained calls I don't understand what to ask in the first or second call. My understanding is, that only the assigning of the view to the new query needs to be executed asynchronous. The loading of list and view should therefore work without an async call.
    – maxkra
    Commented Dec 19, 2017 at 10:14
  • I have updated my answer. Commented Dec 21, 2017 at 15:02

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.