0

I got a list and a list view. But I want to retrieve only particular list view's data.

How can i do that with javascript ?

0

There's a lot of advantages to doing this. The trick is to get the caml query off of the list view before querying the list for items.

var ctx = new SP.ClientContext.get_current(),
    list = ctx.get_web().get_lists().getByTitle(<list title>),
    views = list.get_views(),
    view = views.getByTitle(<view name>),
    items;

ctx.load(views);
ctx.executeQueryAsync(
    function(){
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(view.get_listViewXml());
        items = list.getItems(camlQuery);
        ctx.load(items);
        ctx.executeQueryAsync(
            function(){
                //items variable will be populated with items from view
            },
            function(sender, args){
                console.error('List items could not be retrieved');
            }
        );
    },
    function(){}
);

The down side is that there's 2 execute's in order to make this work. one in order to get the data from the list view, the 2nd one to pass in the query pulled from the specific view.

I have a few gists that I use this on for utility sake.
Delete List View Items

Recycle List View Items

Touch List View Items - Used to Trigger on change workflows for all items in a specific view.

You should be able to pull out the general logic or modify for your own extension method on SP.List

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.