Get Sharepoint List item count using CSOM

Hello All , 

I want to get list item count using CSOM object model ... How can I achieve this ? And which method is efficient ajax webservice call or using Jquery ?

Can anyone share the piece of code ?

February 14th, 2015 9:40pm

Hi,

Please try this using spservices

$().SPServices({
        operation: "GetListItems",
        listName: "Your_list_name",
        async: false,
        completefunc: function(xData, Status) {
            itemCount = $(xData.responseXML).SPFilterNode("rs:data").attr("ItemCount");
        }
    });
    alert(itemCount);
});

Please refer this url for more information "https://spservices.codeplex.com/discussions/534802"

Regards,

Anurag

Free Windows Admin Tool Kit Click here and download it now
February 14th, 2015 11:13pm

Hi,

You can try below code block in CEWP on your page.

<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script>
<script>
    var context;
    var web;
    var list;
    var targetList;
    (function() {

        // This code runs when the DOM is ready and creates a context object which is 
        // needed to use the SharePoint object model
        $(document).ready(function() {
            getItemCount();
        });

        function getItemCount() {
            debugger;
            context = SP.ClientContext.get_current(); //gets the current context
            web = context.get_web(); //gets the web object
            list = web.get_lists(); //gets the collection of lists
            targetList = list.getByTitle("MyListName"); //get the list details
            context.load(targetList);
            context.executeQueryAsync(onGetSuccess, onGetFail);
        }

        function onGetSuccess() {
            console.log("List Item Count= " + targetList.get_itemCount());
        }

        function onGetFail(sender, args) {
            console.log('Error:' + args.get_message());
        }
    })();
</script>
Thanks. Please mark it as answer if it helps.

February 16th, 2015 3:04am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics