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 SharePoint 2010 web site with a master page that has a counter on it. I'm trying to update that counter using the ECMAScript. The code I have falls nicely into the success method - but I cannot find the right call to get a count. I have tried length, length(), count & count(). Does anybody have any suggestions? I have the code working, but only by enumerating through the results of the list and incrementing a count.

This is what I have so far, as an example:-

<script type="text/javascript">
    var clientContext = null;
    var web = null;
    ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
    function Initialize()
    {
        clientContext = new SP.ClientContext.get_current();
        web = clientContext.get_web();
        var list = web.get_lists().getByTitle("Absences");
        var camlQuery = new SP.CamlQuery();
        var q = "<Where><Eq><FieldRef Name='StatusID' /><Value Type='Integer'>1</Value></Eq></Where>";
        camlQuery.set_viewXml(q);
        this.listItems = list.getItems(camlQuery);
        clientContext.load(listItems, 'Include(Id)');
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess), 
        Function.createDelegate(this, this.onQueryFailed));
    }
    function onListItemsLoadSuccess(sender, args) {
        var count = 0;
        var listEnumerator = this.listItems.getEnumerator();
        //iterate though all of the items
        while (listEnumerator.moveNext()) {
            count = count + 1;
        }

        $('#spnNumberOfAbsences').text(count);
    }

    function onQueryFailed(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>
share|improve this question

9 Answers 9

up vote 5 down vote accepted

If you have not got your answer yet, use the below line -

              var count = 0;
    var count = this.listItems.get_count();

The while loop is not required to get the count.

share|improve this answer

Items.get_count();

Will not work if you have a RowLimit in your query. If your query exceeds your RowLimit then the RowLimit will be returned by get_count()

share|improve this answer

The object

Sharepoint.client.List

has a property ItemCount

which will return to you a count of the total items in that list

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

share|improve this answer
    
Sorry, my question wasn't full. I need it in javascript. –  user7268 Mar 8 '12 at 10:03
    
the same model applies to javascript, you can use c# com api for javascript work –  Sebastien Stettler Mar 8 '12 at 10:09

If you get your items in javascript object model, you can use get_count()

items.get_count()

In this case you get only the count of your items, which you have loaded, which is not the same as SPList.ItemCount

share|improve this answer

it appears from the above code that you have declared this.listItems inside your Initialize() method and not globally outside in the script. I believe you will find trouble in accessibg the same. Moreover this.listItems.length should work and give you the count.

share|improve this answer
    
I have - but that is as per the documentation I have been using as an example, and the above code does work. But this.listItems.length returns undefined. –  grimorde Sep 15 '10 at 7:54
    
have u tried to debug the js and see whats actually in that object.. use IEDevloper or firebug to test this script and various objects. –  Vivek Sep 15 '10 at 9:15

try this.get_itemCount();

http://msdn.microsoft.com/en-us/library/ee548354.aspx

share|improve this answer
    
This gives me an error of "Object doesn't support this property or method" –  grimorde Oct 15 '10 at 12:29

You can use SPList.ItemCount or am I not understanding your question?

share|improve this answer
    
I'm sorry, my question wasn't full. I need it in javascript. –  user7268 Mar 8 '12 at 10:07

Have a look at a blog I wrote which demonstrates the usage of Sharepoint Client Object Model. You'll also find some examples where I iterate over all items in a list. Check here : http://pointtoshare.wordpress.com/2011/11/01/using-the-sharepoint-client-object-model-client-om-or-ecma-script-and-jquery-to-display-list-items-any-way-you-want/

share|improve this answer

What you're doing doesn't work. The condition is ignored and you get a count of every item in the list. Taking your code and the responses, this gets count by StatusID (tho it feels a lot like, just because Microsoft can't sell it if it has someone else's name on it, we're throwing out jquery's ajax and reinventing the wheel):

<script type="text/javascript">
    var clientContext = null;
    var web = null;
    ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
    function Initialize()
    {
        clientContext = new SP.ClientContext.get_current();
        web = clientContext.get_web();
        var list = web.get_lists().getByTitle("Absences");
        var camlQuery = new SP.CamlQuery();
        var q = "<View><Query><Where><Eq><FieldRef Name='StatusID' /><Value Type='Integer'>1</Value></Eq></Where></Query><RowLimit>0</RowLimit></View>";
        camlQuery.set_viewXml(q);
        this.listItems = list.getItems(camlQuery);
        clientContext.load(listItems, 'Include(Id)');
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess), 
        Function.createDelegate(this, this.onQueryFailed));
    }
    function onListItemsLoadSuccess(sender, args) {

        var count = this.listItems.get_count();

        alert(count);
    }

    function onQueryFailed(sender, args) {
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>
share|improve this answer

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.