Get SharePoint 2013 List Item Count using Rest API and ECMAScript object model

In this post we will discuss how we can get list item count using SharePoint 2013 Rest API as well as using ECMAScript object model. I have a SharePoint 2013 list and I have added a web part page and in that web part page on a button click I am trying to retrieve list item count by using Rest API.

Related Posts:
SharePoint 2013 Get current user group collection through Rest api

Add Bulk Users to SharePoint 2013 Group from Excel using SharePoint 2013 REST API

Remove User by LoginName from SharePoint 2013 Group using Rest API and Remove User by Id from SharePoint group using Rest API

Get List Item Count using Rest API:
Put the below code in a script editor web part.

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>
<script>
$(function(){
$(“#btnClick”).click(function(){
var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/web/lists/getbytitle(‘MyTestList’)/items”;
$.ajax({
url: requestUri,
type: “GET”,
headers: {
“accept”:”application/json;odata=verbose”,
“content-type”: “application/json;odata=verbose”,
“X-RequestDigest”:$(“#_REQUESTDIGEST”).val()
},
success: onSuccess,
error: onError
});

function onSuccess(data) {
var itemsCount = data.d.results.length;
alert (itemsCount);
}

function onError(error) {

alert(JSON.stringify(error));

}

});

});

</script>

<input type=”button” id=”btnClick” value=”Count List Items”/>

Get List Item Count using ECMAScript client object model:
We can also retrieve list item count using ECMAScript client object model. Put the below code inside a script editor web part.

<script type=”text/javascript”>
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, “sp.js”);
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
var list = web.get_lists().getByTitle(“MyTestList”);
var camlQuery = new SP.CamlQuery();
var q = “<View><Query><Where><Eq><FieldRef Name=’Title’ /><Value Type=’Text’>abc</Value></Eq></Where></Query><RowLimit>0</RowLimit></View>”;
camlQuery.set_viewXml(q);
this.listItems = list.getItems(camlQuery);
clientContext.load(listItems, ‘Include(Title)’);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),
Function.createDelegate(this, this.onQueryFailed));
}
function onListItemsLoadSuccess(sender, args) {

var count = this.listItems.get_count();

alert(count);
}


function onQueryFailed(sender, args) {
alert(‘request failed ‘ + args.get_message() + ‘\n’ + args.get_stackTrace());
}
</script>

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →