Share via


SharePoint online: Retrieve all lists and libraries from SharePoint site using JavaScript object model

Introduction:

Here we will discuss how we can retrieve all lists and libraries from SharePoint site using JavaScript object model in SharePoint online Office 365. The same code will work for SharePoint 2016/2013 also.

Here we have put the code inside a script editor web part inside a web part page. If you face one issue like Uncaught TypeError: SP.ClientContext is not a constructor error, then you can resolve by following this article.

JavaScript Code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
 
<div id="divGetLists"></div>
 
<script>
 
    $(function () {
 
        ExecuteOrDelayUntilScriptLoaded(getAllLists, "sp.js");
 
    });
 
    function getAllLists() {
 
        var clientContext = new SP.ClientContext();
 
        var oWebsite = clientContext.get_web();
 
        this.collList = oWebsite.get_lists();
 
        clientContext.load(collList);
 
        clientContext.executeQueryAsync(
 
        Function.createDelegate(this, this.onQuerySucceeded),
 
        Function.createDelegate(this, this.onQueryFailed)
 
        );
 
    }
 
    function onQuerySucceeded() {
 
        var listInfo = '';
 
        var listEnumerator = collList.getEnumerator();
 
        while (listEnumerator.moveNext()) {
 
            var oList = listEnumerator.get_current();
 
            listInfo += oList.get_title() + '<br />';
 
        }
 
        $("#divGetLists").html(listInfo);
 
    }
 
    function onQueryFailed(sender, args) {
 
        alert('Request failed. ' + args.get_message() +
 
        '\n' + args.get_stackTrace());
 
    }
 
</script>

Once you will save this code it will display all the lists and libraries like below:

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/SharePoint_online_javascript_object_model_get_all_lists.png

References:

If you want to learn Microsoft Flow, you can see below articles.

Conclusion:

Here we have seen how we can retrieve all lists and libraries using JavaScript object model in SharePoint Online.