Question

I want to retrieve all the lists title of my SharePoint site and add in them into a list.

But the error "Uncaught TypeError: Cannot read property 'apply' of undefined at Array." appeared in the console.

Here is my script :

ExecuteOrDelayUntilScriptLoaded(checkPermissions, "sp.js");

function checkPermissions(){

var context = new SP.ClientContext.get_current();
var listObject = context.get_web().get_lists();
context.load(listObject);
context.executeQueryAsync(Function.createDelegate(this, this.onListsQuerySucceeded), Function.createDelegate(this, this.onListsQueryFailed));

function onListsQuerySucceeded() {
    var listEnumerator = lists.getEnumerator();
    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        var oListTitle = oList.get_title();
        //Ajout du titre de la liste dans la liste "Permissions Rights"
        var lstObject = lists.getByTitle("Droits d'accès");
        var listItemCreationInfo = new SP.ListItemCreationInformation();
        var newItem = lstObject.addItem(listItemCreationInfo);
        newItem.set_item('Titre', oListTitle);
        newItem.update();
    }
}  

function onListsQueryFailed(sender,args) {
    alert("Failed: "+ args.get_message());      
}
}

I don't know why and where?

Could someone help me please?

UPDATE :

This code works thanks to the answers :

ExecuteOrDelayUntilScriptLoaded(function() {
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
var lists = web.get_lists();
clientContext.load(lists);
clientContext.executeQueryAsync(onListsQuerySucceeded, onListsQueryFailed);
var lstObject = clientContext.get_web().get_lists().getByTitle("Droits d'accès");

function onListsQuerySucceeded(sender, args) {
    var listEnumerator = lists.getEnumerator();
    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        var oListTitle = oList.get_title();
        //console.log("List Title: " + oListTitle);
        var listItemCreationInfo = new SP.ListItemCreationInformation();
        var newItem = lstObject.addItem(listItemCreationInfo);
        newItem.set_item('Title', oListTitle);
        newItem.update();
    }
}  


function onListsQueryFailed(sender, args) {
    console.log("Failed: "+ args.get_message());
}
},"sp.js");
Was it helpful?

Solution

I can see following issues in the code: In the line

 var listEnumerator = lists.getEnumerator();

the variable 'lists' is undefined, it is not assigned/declared in your code. Replace

var listObject=context.get_web().get_lists();

with

lists=context.get_web().get_lists();

where lists is a global variable The line

 var lstObject = lists.getByTitle("Droits d'accès"); 

should be outside the while loop.

Also, you need to call context.ExecuteQueryAsync(successMethod,failureMethod) after adding the items to the list, i.e after the update statement.

Refer here: https://docs.microsoft.com/en-us/previous-versions/office/developer/sharepoint-2010/hh185011(v=office.14)

OTHER TIPS

You can use below code to get all list titles from SharePoint site using JSOM:

ExecuteOrDelayUntilScriptLoaded(function() {
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web();
    var lists = web.get_lists();
    clientContext.load(lists);
    clientContext.executeQueryAsync(onListsQuerySucceeded, onListsQueryFailed);

    function onListsQuerySucceeded(sender, args) {
        var listEnumerator = lists.getEnumerator();
        while (listEnumerator.moveNext()) {
            var oList = listEnumerator.get_current();
            var oListTitle = oList.get_title();
            console.log("List Title: " + oListTitle);
        }
    }  

    function onListsQueryFailed(sender, args) {
        console.log("Failed: "+ args.get_message());
    }
},"sp.js");

Reference:

  1. How to: Retrieve Lists Using JavaScript
  2. How to get all the list names in a site from the client side?
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top