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."
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());
}
  • add 4 space or tab to have lines formatted as code. Not only reads better but improves SO search as well – Danny '365CSI' Engelman Feb 14 '17 at 19:09
  • Thanks for your answer ! Unfortunetely I receive cowebEnumerator is undefined. – ameliapond Feb 15 '17 at 9:01
  • check edited answer – Samir Khimani Feb 15 '17 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. – ameliapond Feb 15 '17 at 9:42
  • use above code...Have you use CSOM or JSOM? – Samir Khimani Feb 15 '17 at 9:46

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.