SharePoint - How to display all SP libraries from all sub sites using javascripts.

Asked By Rahul Khanna on 16-Apr-17 11:59 AM
Hi,
I am displaying all sub sites and their document library(not any other list) under the particular sub site. I am able to get the sub sites but document libray returning only one from the first web. Debugged and found that "while (listEnumerator.moveNext())" loop is executing only one time, but it has to loop thru all the document libraries for each sub sites. Here is my complete code:

     <div id="LKMSubWebs"> 
     </div>
    <script type="text/javascript">
     var HTMLContent = '';
     ExecuteOrDelayUntilScriptLoaded(RetrieveSites,"sp.js");
     function RetrieveSites()
     {
       currentcontext = new SP.ClientContext.get_current();
       currentweb = currentcontext.get_web();
      //this.subsites = currentweb.get_webs();
      this.subsites = currentweb.getSubwebsForCurrentUser(null);
      currentcontext.load(this.subsites);
      currentcontext.executeQueryAsync(Function.createDelegate(this, 
      this.SitesExecuteOnSuccess),
      Function.createDelegate(this, this.SitesExecuteOnFailure));
     }

     function SitesExecuteOnSuccess(sender, args) 
    {
      //var HTMLContent = '';
      var enum1 = this.subsites.getEnumerator();
      el = document.getElementById('LKMSubWebs');

     while (enum1.moveNext())
     {
      var Site = enum1.get_current();
      //alert('hi123');
      //Add Subsites to HTML Content
      HTMLContent += '\n' + 
      '<img title="css" alt="css" 
      src="/_layouts/15/images/SharePointFoundation16.png" border="0">&nbsp;' + 
      '<a class=subsitecls href='+ 
      Site.get_serverRelativeUrl()+'>'+Site.get_title()+'</a> </br>'
     //RootElement.append('<a class=subsitecls href='+ 
     siteTitle+'>'+siteTitle+'</a> </br>');
       //alert(HTMLContent);
       }
     //set div to content
     el.innerHTML = HTMLContent;
      retrieveAllListProperties();
       }
      function SitesExecuteOnFailure(sender, args) 
      {
       alert("error");
       //alert(args.get_message());
       }

        //+++++++++++++++++++++++++++

     function retrieveAllListProperties()
     {
        alert("Inside function")
        clientContext = new SP.ClientContext.get_current();//(siteUrl);
        var oWebsite = clientContext.get_web();
        lists = oWebsite.get_lists();
        //listRootFolder = lists.get_rootFolder();
        clientContext.load(lists);
   //clientContext.load(lists, 'Include(DefaultViewUrl)');
       //clientContect.Load(listRootFolder);
       clientContext.executeQueryAsync(Function.createDelegate(this, 
      this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
     }

      function onQuerySucceeded()
     {

    var listInfo = '';

    var listTemplateId="";

    var listEnumerator = lists.getEnumerator();   

    while (listEnumerator.moveNext())
     {
        var oList = listEnumerator.get_current();
        //listTemplateId+=oList.get_baseTemplate()+'\n';

        alert("Inside listEnumerator" +oList.get_baseTemplate())

         //if(oList.get_baseTemplate() == "101")
            //{ 
                //alert("Inside template function")
                //alert("olist" +oList.get_title())
            //}
            
        //var oListRootFolder = oList.get_rootFolder()
        //clientContext.load(oListRootFolder);
        //clientContext.ExecuteQuery();
        //oListRootFolder.get_serverRelativeUrl()
        var str = oList.get_defaultViewUrl();

        //alert(str)
        //listInfo += 'Title: ' + oList.get_title() + ' Url: '+ str +' Created: ' + oList.get_created().toString() + '\n';
//alert(oList.get_title())
//listInfo += 'Title: ' + oList.get_title() + ' Url: '+ str +' '\n';
//listInfo += 'Title: ' + oList.get_title();
    }
    //alert(listTemplateId)
    //alert(listInfo);
   }

      function onQueryFailed(sender, args) {
      alert('Request failed. ' + args.get_message() + '\n' + 
     args.get_stackTrace());
    }

   </script>