I have the following code:

var markup;

function generateTree() {
    var lines = document.getElementById("imputs").value.split('\n');
    var targetWeb;
    var clientContext;
    var contexto = window.location.protocol + "//" + window.location.host;

    for (var i = 0; i < lines.length; i++) {
        var path = lines[i].split(contexto);
        clientContext = new SP.ClientContext(path[1]);            

        var webs = clientContext.get_web().getSubwebsForCurrentUser(null);
        clientContext.executeQueryAsync(this.onQuerySucceeded(webs), this.onQueryFailed);            
        markup += '<br>';

    results.innerHTML = markup;
    markup = '';

function onQuerySucceeded(webs) {
    for (var j = 0; j < webs.length; j++) {
        markup += webs[j].get_title() + '<br>';

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());

But debugging I see that the executeQueryAsync statement doesn't execute the success or the fail method, it just follows executing "markup += '<br>';"


This is because you don't pass a function reference as first argument to executeQueryAsync. Since you call this.onQuerySucceeded(webs) (note the parentheses) you actually pass in the result of that function as first argument. Since that function does not return anything, that result would be undefined.

  • +1 correct answer, you need to supply a reference not a call. So you have to change this.onQuerySucceeded(webs) to onQuerySucceeded. this.onQueryFailed is a reference. If you do not get the alert it never failed.. – Danny '365CSI' Engelman Oct 21 '15 at 20:09

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.