Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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:
References:
If you want to learn Microsoft Flow, you can see below articles.
- Microsoft Flow Example Save tweets that include specific hashtag to a SharePoint list
- Working with Microsoft Flow in SharePoint Online Office 365 and Demo on Send a customized email when a new SharePoint list item is added
Conclusion:
Here we have seen how we can retrieve all lists and libraries using JavaScript object model in SharePoint Online.