Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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

share|improve this answer

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

oList.ItemCount has your property.

share|improve this answer
    
Hi this is javascript, not server side. –  IanCian Aug 19 '11 at 16:16
    
The error is that it is telling me that the list is not initialized. so i tried the following before executing var oList = oWeb.get_lists().getById(sListId); item_clientContext.load(oList); item_clientContext.executeQueryAsync(); var count = oList.get_itemCount(); –  IanCian Aug 19 '11 at 16:45
    
still with problem of not initialized –  IanCian Aug 19 '11 at 16:46
    
@IanCian: So oList is null? Did you try var count = oList.ItemCount;? –  Kit Menke Aug 19 '11 at 19:27
1  
oList will be initialized in onSuccess callback –  Andrey Markeev Aug 19 '11 at 20:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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