2

I have a list with a lot of items in it I need to know the list item count the following is my code:

var sListId = SP.ListOperation.Selection.getSelectedList();
var oWeb = item_clientContext.get_web();
var oList = oWeb.get_lists().getById(sListId);
var count = ???

How can I determine the count of items I have? I tried get_count() but did not work.

Kit Menke
  • 4,193
  • 6
  • 32
  • 40
IanCian
  • 256
  • 1
  • 5
  • 18

2 Answers2

5

Since you're using the JavaScript CSOM you need to fetch the properties asynchronously. Your code should look something like this:

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());
}

GL

1

See here http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.list.itemcount.aspx

oList.ItemCount has your property.

Tim Gabrhel
  • 2,325
  • 2
  • 24
  • 46