2

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");
1

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)

| improve this answer | |
  • Thanks for your response ! It works! I will put my code in my question. – TLAsker Jul 3 at 12:26
2

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?
| improve this answer | |
  • Thanks for your answer Ganesh ! Your solution works but I didn't wan't to have them in the console but to add each title in a list. It works thanks to the answer of Sourav. I will pu my code. – TLAsker Jul 3 at 12:25
  • ok, I can provide the code to add list titles in another list if you want. – Ganesh Sanap Jul 3 at 12:29
  • With the code that I have put in my question, adding the list titles in another list already works. Thanks for your help :) – TLAsker Jul 3 at 12:34
  • Great, keep using this platform for your queries and questions. – Ganesh Sanap Jul 3 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.