This SharePoint tutorial explains, how to get list item count using SharePoint 2013/2016/Online 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.
Get SharePoint List Item Count using Rest API
We will see how we to get SharePoint list item count using Rest API in SharePoint Online/2013. You can put the below code in a script editor web part or content editor web part.
<script src="https://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 SharePoint List Item Count using ECMAScript client object model
We can also retrieve list item count using ECMAScript client object model in SharePoint. Put the below code inside a script editor web part or content 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>
You may like following SharePoint Rest API tutorials:
- Retrieve SharePoint list items programmatically using jsom, rest API and csom in SharePoint Online/2016/2013
- SharePoint Branding: Displaying dynamic contents into a page using JavaScript and REST API in SharePoint online or SharePoint 2016/2013
- Bind SharePoint list items to the dropdown list using javascript object model (jsom), Rest API and Server Object Model
- Get Current User Details Using JavaScript Object Model (jsom) and jQuery in SharePoint 2013/2016/Online
- Retrieve and Display TASK status using REST API in SharePoint
- Create Highcharts in SharePoint using Rest API
- Calling Rest API from A SharePoint 2013 Designer Workflow
- How to delete all items from SharePoint online list using Rest API?
- How to use Deferred promise and then in Rest API in SharePoint Online or SharePoint 2013?
Hope this will be helpful to get SharePoint 2013 List Item Count using Rest API and ECMAScript object model.
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 SPGuides.com