2

I am wanting to count the number of items in my list. Using CSOM in javascript I have tried it but my code doesn't work. Where am I going wrong.

Here is my code:

var clientContext = new SP.ClientContext(siteUrl);
var oWebsite = clientContext.get_web();
this.collList = oWebsite.get_lists();

list = collList.getByTitle('Urgent Alerts');
var items = list.getItems();
clientContext.load(items);
clientContext.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failed))

function Success(sender, args) {
var itemCount = items.get_count();
alert(itemCount);
}

function Failed(sender, args) {
alert('request failed ' + args.get_message() + 'n' + args.get_stackTrace());
}
1

3 Answers 3

5

You need to call context.executeQueryAsync And then try to get the item count in the success method.

UPDATE

You are not specifying the query in SP.List.getItems(query_); Example:

var clientContext = new SP.ClientContext(siteUrl);
var oWebsite = clientContext.get_web();
this.collList = oWebsite.get_lists();

list = collList.getByTitle('Urgent Alerts');
var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
var items = list.getItems(camlQuery);
clientContext.load(items);
clientContext.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failed))

function Success(sender, args) {
    var itemCount = items.get_count();
alert(itemCount);
}

function Failed(sender, args) {
    alert('request failed ' + args.get_message() + 'n' + args.get_stackTrace());
}
3
  • So what query should I use?
    – dave
    Mar 21, 2014 at 10:20
  • I am not querying anything
    – dave
    Mar 21, 2014 at 10:21
  • If you are not querying anything you need to simply load the list. list.getItems() expects a CamlQuery object as paramter. Mar 21, 2014 at 10:38
1

Use This link

var oList;
function theFunction() {
    var sListId = SP.ListOperation.Selection.getSelectedList();
    var oWeb = item_clientContext.get_web();
    oList = oWeb.get_lists().getById(sListId);

    // .load() tells CSOM to load the properties of this object
    // multiple .load()s can be stacked
    item_clientContext.load(oList);

    // now start the asynchronous call and perform all commands
    item_clientContext.executeQueryAsync(onSuccess, onFail);
    // method will exit here and onSuccess or OnFail will be called asynchronously
}
function onSuccess(sender, args) {
    alert('No of rows: ' + oList.get_itemCount());
}
function onFail(sender, args) {
    alert('Request failed.\n' + args.get_message() + '\n' + args.get_stackTrace());
}
0

Try modifying your code with this bit :

list = collList.getByTitle('Urgent Alerts');
this.items = list.getItems();
clientContext.load(items);
clientContext.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failed))


function Success(sender, args) {
    var itemEnumerator= this.items.getEnumerator();
    var itemCOunt = itemEnumerator.$2K_0;
    alert(itemCount);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.