Tuesday, 11 March 2014

Get All items From a Particular List View in SharePoint CSOM Javascript

Get All items From a Particular List View in SharePoint CSOM Javascript
function getItemsFromView(listTitle, viewTitle)
{
var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(listTitle);
var view = list.get_views().getByTitle(viewTitle);
context.load(view);
context.executeQueryAsync(
function(sender, args) {getItemsFromList(listTitle, "<View><Query>" + view.get_viewQuery() + "</Query></View>")},
function(sender, args) {alert("error: " + args.get_message());}
);
}
function getItemsFromList(listTitle, queryText)
{
var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(listTitle);
var query = new SP.CamlQuery();
query.set_viewXml(queryText);
var items = list.getItems(query);
context.load(items);
context.executeQueryAsync(
function()
{
var listEnumerator = items.getEnumerator();
var i = 0;
while (listEnumerator.moveNext())
{
i++;
}
alert("items retrieved: " + i);
},
function(sender, args) {alert("error in inner request: " + args.get_message());}
);
}
// example of usage
getItemsFromView("Tasks", "My Tasks");