Get list items from view using JavaScript (jsom) in SharePoint Online/2013/2016

This SharePoint tutorial, we will discuss how to get list items from view using JavaScript (jsom) in SharePoint Online or SharePoint 2013/2016.

Here I will show, how we can retrieve list items from a particular view in SharePoint using JavaScript Object Model code.

SharePoint JavaScript get list items from view

Below is the jsom code to get list items from view, which you can write inside a script editor web part or a content editor web part.

<script>
SP.SOD.executeOrDelayUntilScriptLoaded(getItemsFromView,"sp.js");

function getItemsFromView(listTitle, viewTitle)
{
var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle("DemoList");
var view = list.get_views().getByTitle("My Test view");
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());}
);
}
</script>

You may also like following SharePoint jsom tutorials:

Once you run the above code, you can see it will display all the items from the list view in SharePoint.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>