0

I would like to know how to get subweb lists using CSOM. I actually know how to get lists on a web and how to get a sub web collection but I can't figure out how to get lists on sub web collection.

Let's say we have the following web hierarchy.

http://www.web/web1
http://www.web/web2
http://www.web/...

How to iterate on web1, web2, etc and getting for example the list http://www.web/web42/Lists/myPrecious/. ?

I have been that far:

function displayLists() {
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    webCollection = web.getSubwebsForCurrentUser(null);
    context.load(webCollection);        
    context.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, error));
}

function success() {    
    var webEnumerator = webCollection.getEnumerator();  
    while (webEnumerator.moveNext())
    {           
        var web = webEnumerator.get_current();
        var list = web.get_lists().getByTitle("myPrecious");    
        var title = list != null ? list.get_title() : "nothing";            
        console.log(title);
    }           
}

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

I'm getting the error

"The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."

1 Answer 1

0

You will need to load and context.executequry() for getting list information of subweb.

Use below code in while loop.

function displayLists() {
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    webCollection = web.getSubwebsForCurrentUser(null);
    context.load(webCollection);        
    context.executeQueryAsync(Function.createDelegate(this, success), Function.createDelegate(this, error));
}

function success() {    
    var webEnumerator = webCollection.getEnumerator();  
    while (webEnumerator.moveNext())
    {           
       var innercontext = webEnumerator.get_current();
     var  innerweb= innercontext.get_web();        
     var list = innerweb.get_lists().getByTitle("myPrecious");
     innercontext.load(list);
     innercontext.ExecuteQuery();
     var title = list != null ? list.get_title() : "nothing";            
     console.log(title);
    }           
}

function error(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}
10
  • add 4 space or tab to have lines formatted as code. Not only reads better but improves SO search as well Commented Feb 14, 2017 at 19:09
  • Thanks for your answer ! Unfortunetely I receive cowebEnumerator is undefined. Commented Feb 15, 2017 at 9:01
  • check edited answer Commented Feb 15, 2017 at 9:04
  • Thanks but now I have Unable to get property 'undefined' of undefined or null reference. Just by commenting all the script except the first line. Commented Feb 15, 2017 at 9:42
  • use above code...Have you use CSOM or JSOM? Commented Feb 15, 2017 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.