none
Get all List items from Subsites using ECMAScript? RRS feed

Answers

  • Hi Jamie,

    Thank you very much for working on it.

    I tried up a lot of solutions and finally got the expected result.

    Here is the code that worked for me

    <script type="text/javascript" language="javascript">
    
         ExecuteOrDelayUntilScriptLoaded(RetrieveListItems, "sp.js");
         
         var clientContext = null;
         var spWebobj = null;
         var spListobj = null;
         var liCount;
         var items;
         var listItemInfo = '';  
         
         function RetrieveListItems() {
    
             clientContext = new SP.ClientContext.get_current();
    
             this.site = clientContext.get_web();
    
             this.webSubSites = site.get_webs();
    
             clientContext.load(this.webSubSites, 'Include(Title, ServerRelativeUrl)');
    
             clientContext.executeQueryAsync(Function.createDelegate(this, this.onLoadSubWebs), Function.createDelegate(this, this.onQueryFailed));
    
         }
    
         function onLoadSubWebs(sender, args) {
    
             items = [];              
     
             for (var x = 0; x < this.webSubSites.get_count(); x++) {        
    
                 web = this.webSubSites.itemAt(x);
                 
                 var siteUrl = web.get_serverRelativeUrl();             
    
                     spListobj = web.get_lists().getByTitle("Tasks");
    
                     var spCamlQuery = new SP.CamlQuery();               
    
                     var spViewXML = '<View><Query><Where><And><Leq><FieldRef Name="DueDate"/><Value Type="DateTime"><Today OffsetDays="-3"/></Value></Leq><Neq><FieldRef Name="Status"/><Value Type="Text">Completed</Value></Neq></And></Where></Query></View>'; 
                     
                     spCamlQuery.set_viewXml(spViewXML);
    
                     var spListobjItems = spListobj.getItems(spCamlQuery);                                 
    
                     var itemDetails = new Object;
    
                     itemDetails.ListItems = spListobjItems;
                     itemDetails.WebTitle = web.get_title();
                     itemDetails.TaskListOverDueViewURL = siteUrl + '/Lists/Tasks/';
    
                     items.push(itemDetails);
    
                     clientContext.load(spListobjItems, 'Include(Id, FileDirRef)');     
                                                                                    
                }
    
             clientContext.executeQueryAsync(Function.createDelegate(this, this.onLoadTaskDetails), Function.createDelegate(this, this.onQueryFailed));
         }
    
         function onLoadTaskDetails(sender, args) {
    
             var htmlGetTitle = "<ul>";
    
             var htmlGetCount = "<ul>";        
             
             for (var x = 0; x < items.length; x++) {            
    
                     var itemDetails = items[x];
                     
                     htmlGetCount += "<li>" + itemDetails.ListItems.get_count() + "</li>";
    
                     htmlGetTitle += "<li> <a href='" + itemDetails.TaskListOverDueViewURL + " ' target=_blank'> <u>" + itemDetails.WebTitle + "</u> </a> </li>";             
             }
             document.getElementById('divSiteCount').innerHTML = htmlGetCount;
             document.getElementById('divSiteTitle').innerHTML = htmlGetTitle;        
            }
     
    
         function onQueryFailed(sender, args) {
    
             alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
         }
    
          </script>   
    
    <table> 
         <tr>
           <th>Project</th>
           <th>Exceptions</th>
       </tr>
       <tr id="divSiteListings"> 
           <td> <div id="divSiteTitle"></div></td>     
           <td> <div id="divSiteCount"></div></td>   
       </tr> 
      </table>

    Thanks once again for your efforts.
    Thursday, July 12, 2012 6:17 AM

All replies

  • I'd say this is promising;

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


    w: http://www.the-north.com/sharepoint | t: @JMcAllisterCH | c: http://www.b-i.com

    Thursday, July 5, 2012 10:12 AM
  • Hi Jamie,

    Thanks for quick reply, i went through this MSDN article.

    It just shows to retrive List items from a single Site and not from multiple sites or subsites.

    My requirement is to get List items from subsites.

    Need help on how to use the same code as shown in this MSDN article to retrieve list items from all subsites.

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

    Thursday, July 5, 2012 10:24 AM
  • It's not a bad conversion. You already get the webs collection of each web in your original script, so it's a question of introducing some recursion and for each web calling retrieveListItems method in the script I provided.   


    w: http://www.the-north.com/sharepoint | t: @JMcAllisterCH | c: http://www.b-i.com

    Thursday, July 5, 2012 11:35 AM
  • Hi Jamie,

    Sorry don't know much about ECMAScript code, can you provide me with some code sample for this.

    Thanks for your help.

    Thursday, July 5, 2012 12:34 PM
  • Earliest I can look at this is 8pm tonight. I'll see if I get the opportunity and let you know.

    w: http://www.the-north.com/sharepoint | t: @JMcAllisterCH | c: http://www.b-i.com

    Thursday, July 5, 2012 12:45 PM
  • Thanks Jamie, appreciate your help.

    Thursday, July 5, 2012 1:06 PM
  • Still looking for help on retrieving List items from Subsites using ECMAScript.
    Friday, July 6, 2012 10:27 AM
  • It's on my ToDo List. :)

    w: http://www.the-north.com/sharepoint | t: @JMcAllisterCH | c: http://www.b-i.com

    Friday, July 6, 2012 10:39 AM
  • Thinking about the issue, SPSiteDataQuery might be more efficient than recursion. I'm rushing for a plane right now, however I dug up this script;

    var _site = new SPSite("http://testcollection/test");
    var _web = _site.OpenWeb();
    var siteQuery = new SPSiteDataQuery();
    siteQuery
    .Webs = "<Webs Scope=\"SiteCollection\" />";
    siteQuery
    .Lists = "<Lists ServerTemplate=\"101\" BaseType=\"1\" MaxListsLimit=\"0\"/>";
    siteQuery
    .ViewFields = "<FieldRef Name=\"LinkFilename\" />";
    System.Data.DataTable dt = _web.GetSiteData(siteQuery);
    var count = dt.Rows.Count;

    from here; http://sharepoint.stackexchange.com/questions/28616/spsitedataquery-is-not-returning-items-from-a-list-that-have-item-level-security


    w: http://www.the-north.com/sharepoint | t: @JMcAllisterCH | c: http://www.b-i.com

    Friday, July 6, 2012 12:23 PM
  • Hi Jamie,

    Thanks for your time.

    SPSiteDataQuery is great for querying list items from multiple sites.

    But unfortunately i need a querying mechanism using Client Object Model, as my requirement doesn't allow Visual Studio solution to deploy.

    I will be using Content Editor webpart to add the Client Object Model code into it.

    That's why i was trying ECMAScript code that can be easily added to content editor webpart.

    Really appreciate your help. Thanks much.

    Friday, July 6, 2012 1:20 PM
  • Progress, but not there yet. Just to let you know I haven't forgotten you. This is partially working, except for recent edits. This is for Announcements list rather than tasks - which suits my test environment.

    <script>
    
     _spBodyOnLoadFunctionNames.push('start');
    
    
    
    function start()
    {
    
    	var clientContext = new SP.ClientContext.get_current(); 
     
    	var web = clientContext.get_web(); 
    
    	clientContext.load(web);
    
    	retrieveListItems(web);
    
    }
    
    
    function retrieveListItems(web) {
    	
    
        var oList = web.get_lists().getByTitle('Announcements');
            
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
            '<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
        this.collListItem = oList.getItems(camlQuery);
            
        clientContext.load(collListItem);
            
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        
           
    	var subsites = web.get_webs(); 
    
    	clientContext.load(subsites);
    	
    	var webEnum = webs.getEnumerator();
    
    	while (webEnum.moveNext()) {
            	retrieveListItems(webEnum.get_current());
            
    	}
     
    }
    
    function onQuerySucceeded(sender, args) {
    
        var listItemInfo = '';
    
        var listItemEnumerator = collListItem.getEnumerator();
            
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();
            listItemInfo += '\nID: ' + oListItem.get_id() + 
                '\nTitle: ' + oListItem.get_item('Title') + 
                '\nBody: ' + oListItem.get_item('Body');
        }
    
        alert(listItemInfo.toString());
    }
    
    function onQueryFailed(sender, args) {
    
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    </script>


    w: http://www.the-north.com/sharepoint | t: @JMcAllisterCH | c: http://www.b-i.com

    Wednesday, July 11, 2012 3:25 PM
  • Hi Jamie,

    Thank you very much for working on it.

    I tried up a lot of solutions and finally got the expected result.

    Here is the code that worked for me

    <script type="text/javascript" language="javascript">
    
         ExecuteOrDelayUntilScriptLoaded(RetrieveListItems, "sp.js");
         
         var clientContext = null;
         var spWebobj = null;
         var spListobj = null;
         var liCount;
         var items;
         var listItemInfo = '';  
         
         function RetrieveListItems() {
    
             clientContext = new SP.ClientContext.get_current();
    
             this.site = clientContext.get_web();
    
             this.webSubSites = site.get_webs();
    
             clientContext.load(this.webSubSites, 'Include(Title, ServerRelativeUrl)');
    
             clientContext.executeQueryAsync(Function.createDelegate(this, this.onLoadSubWebs), Function.createDelegate(this, this.onQueryFailed));
    
         }
    
         function onLoadSubWebs(sender, args) {
    
             items = [];              
     
             for (var x = 0; x < this.webSubSites.get_count(); x++) {        
    
                 web = this.webSubSites.itemAt(x);
                 
                 var siteUrl = web.get_serverRelativeUrl();             
    
                     spListobj = web.get_lists().getByTitle("Tasks");
    
                     var spCamlQuery = new SP.CamlQuery();               
    
                     var spViewXML = '<View><Query><Where><And><Leq><FieldRef Name="DueDate"/><Value Type="DateTime"><Today OffsetDays="-3"/></Value></Leq><Neq><FieldRef Name="Status"/><Value Type="Text">Completed</Value></Neq></And></Where></Query></View>'; 
                     
                     spCamlQuery.set_viewXml(spViewXML);
    
                     var spListobjItems = spListobj.getItems(spCamlQuery);                                 
    
                     var itemDetails = new Object;
    
                     itemDetails.ListItems = spListobjItems;
                     itemDetails.WebTitle = web.get_title();
                     itemDetails.TaskListOverDueViewURL = siteUrl + '/Lists/Tasks/';
    
                     items.push(itemDetails);
    
                     clientContext.load(spListobjItems, 'Include(Id, FileDirRef)');     
                                                                                    
                }
    
             clientContext.executeQueryAsync(Function.createDelegate(this, this.onLoadTaskDetails), Function.createDelegate(this, this.onQueryFailed));
         }
    
         function onLoadTaskDetails(sender, args) {
    
             var htmlGetTitle = "<ul>";
    
             var htmlGetCount = "<ul>";        
             
             for (var x = 0; x < items.length; x++) {            
    
                     var itemDetails = items[x];
                     
                     htmlGetCount += "<li>" + itemDetails.ListItems.get_count() + "</li>";
    
                     htmlGetTitle += "<li> <a href='" + itemDetails.TaskListOverDueViewURL + " ' target=_blank'> <u>" + itemDetails.WebTitle + "</u> </a> </li>";             
             }
             document.getElementById('divSiteCount').innerHTML = htmlGetCount;
             document.getElementById('divSiteTitle').innerHTML = htmlGetTitle;        
            }
     
    
         function onQueryFailed(sender, args) {
    
             alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
         }
    
          </script>   
    
    <table> 
         <tr>
           <th>Project</th>
           <th>Exceptions</th>
       </tr>
       <tr id="divSiteListings"> 
           <td> <div id="divSiteTitle"></div></td>     
           <td> <div id="divSiteCount"></div></td>   
       </tr> 
      </table>

    Thanks once again for your efforts.
    Thursday, July 12, 2012 6:17 AM
  • Excellent news! Sorry about the late response from me, this all coincided with a trip overseas and some heavy commitments! 

    Glad you are sorted, and thanks for posting the finished script.


    w: http://www.the-north.com/sharepoint | t: @JMcAllisterCH | c: http://www.b-i.com

    Thursday, July 12, 2012 7:00 AM
  • Hi Jamie,

      What if one was looking for a specific subsite rather than enumerating through all subsites?  How would that work?   I'm looking to create an item in a specific list in a specific subsite.  Your guidance is greatly appreciated!

    Tuesday, November 5, 2013 4:34 PM