Getting Count of List Items based on a specific condition using JavaScript in SharePoint 2013

Hi,

Below is the sample script we used to get the total listitems that have MarkAsUnread ( Yes/No) Column value set as No (i.e. 0)


var myItems;

function GetCount() {
 debugger;
 var queryListItem = '<View><Query><Where><Eq><FieldRef Name="MarkAsRead" /><Value Type="Boolean">0</Value></Eq></Where></Query></View>';

 var siteUrl = window.location.protocol + "//" + window.location.host;
 var clientContext = new SP.ClientContext(siteUrl);
 var oList = clientContext.get_web().get_lists().getByTitle('Notification Centre');

var myquery = new SP.CamlQuery();
 myquery.set_viewXml(queryListItem);
 myItems = oList.getItems(myquery);

clientContext.load(myItems);

clientContext.executeQueryAsync(
 Function.createDelegate(this, this.onQuerySucceeded),
 Function.createDelegate(this, this.onQueryFailed)
 );
}

function onQuerySucceeded(sender, args) {

alert(myItems.get_count());
}

function onQueryFailed(sender, args) {

 alert('Request failed. ' + args.get_message() +'\n' + args.get_stackTrace());
}

Hope it helps


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

3 thoughts on “Getting Count of List Items based on a specific condition using JavaScript in SharePoint 2013”

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s