Search This Blog

Friday, March 13, 2015

How to get the count of List items in List using ECMA Script in SharePoint 2010 ,2013


Before client object model introduced . if we want to implement the same concept we  should have to write server side  code  in C# but now we can  get the items count without touching the visual studio.

Now we are going to look at.

1. Creation of Site pages .
2. How to add a content editor web part.
3. How to write Script in Content Editor webpart.

Step :1 Create a site page in  Sharepoint site .




 Note : Now you are done from out of box side created page in site where SharePoint List there.
Step :2  Add content editor webpart into the page by selecting
               
    --> Edit Page

     -->Add web part

    --> webpart gallery

 --> Media and Content (Category)

                                                                                                                                                                     
-->Content editor Web part .


-->Add the Script inside the Html, Source.


-->  You cane See the  Items count  inside the list




Script Description :

<script type="text/javascript">

ExecuteOrDelayUntilScriptLoaded(MainFunction, "sp.js"); 
  
var objContext = null; 
var objWeb = null     
var objList = null;
var objItem = null;

    function MainFunction() {   
        objContext = new SP.ClientContext.get_current();
        objWeb = objContext.get_web();   
        objList = objWeb.get_lists().getByTitle("Product"); //  Here we are passing the list name
  
        objContext.load(objList);

        objContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFail));   
    }   

    function onSuccess(sender, args) {
        alert('Item Count: ' + objList.get_itemCount());
    }   

    function onFail(sender, args) {   
        alert('Some error has occured.');   
    }  
 </script>

 ExecuteOrDelayUntilScriptLoaded(MainFunction, "sp.js");


This functions stops loading the script until the page completly gets loaded . 


Once the page loaded completly  then it acts as a main function to start the script running.

sp.js

This is the heart file for ecma script . it loads from  layout folder in 14 hive . if it does not loads then we  cant work with ECMA Script .
This  is a sample  of ECMA Script with SharePoint .In our netx post we will see how to work with ECMA Script using Visual Webpart.

No comments:

Post a Comment