14

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>
grimorde
  • 547
  • 2
  • 6
  • 20

9 Answers9

13

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

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

or in the case of a returned async query, do not use "this".

    var count = 0;
    var count = listItems.get_count();
Sabel
  • 159
  • 5
5

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

Reggie
  • 51
  • 1
  • 1
3

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

Sebastien Stettler
  • 1,270
  • 1
  • 13
  • 28
2

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

Anatoly Mironov
  • 5,750
  • 2
  • 29
  • 56
0

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>
user33518
  • 11
  • 1
0

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

Vegard
  • 71
  • 1
  • 7
0

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/

Fox
  • 2,676
  • 1
  • 18
  • 30
0

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.

Vivek
  • 1,511
  • 13
  • 20
  • 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 07: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 09:15
0

try this.get_itemCount();

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

Brian Brinley
  • 179
  • 1
  • 6