locked
Get Sharepoint List item count using CSOM RRS feed

  • Question

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

    Saturday, February 14, 2015 6:40 PM

Answers

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

    Monday, February 16, 2015 7:58 AM
  • 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

    Saturday, February 14, 2015 8:13 PM

All replies

  • 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

    Saturday, February 14, 2015 8:13 PM
  • 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.

    Monday, February 16, 2015 7:58 AM
  • Hi,

    You can also try REST api for do the same, try the link below

    https://amalhashim.wordpress.com/2014/12/23/sharepoint-2013-read-list-items-using-rest-api/



    Thanks, Shakir | Please 'propose as answer' if it helped you, also 'vote helpful' if you like this reply.
    Tuesday, February 17, 2015 9:13 AM