13

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>
12

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

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

  • 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
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

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.

  • 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
0

try this.get_itemCount();

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

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

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

  • I'm sorry, my question wasn't full. I need it in javascript. – user7268 Mar 8 '12 at 10:07
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/

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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