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());
}
dave
  • 195
  • 2
  • 3
  • 13

3 Answers3

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());
}
Nadeem Yousuf-AIS
  • 18,707
  • 4
  • 28
  • 59
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());
}
SharePointMan
  • 3,364
  • 5
  • 33
  • 51
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);
}
Amit
  • 1,028
  • 7
  • 22