1

I have loop were I display 3 subsites, the count gets 3 when it finish and it display the last value

$(document).ready(function () {
    //Get the URI decoded URLs.
    hostweburl =
        decodeURIComponent(
            getQueryStringParameter("SPHostUrl")
    );
    appweburl =
        decodeURIComponent(
            getQueryStringParameter("SPAppWebUrl")
    );

    // resources are in URLs in the form:
    // web_url/_layouts/15/resource
    var scriptbase = hostweburl + "/_layouts/15/";

    // Load the js files and continue to the successHandler
    $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
});

// Function to prepare and issue the request to get
//  SharePoint data
function execCrossDomainRequest() {
    // executor: The RequestExecutor object
    // Initialize the RequestExecutor with the app web URL.
    var executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync(
        {          
            url:appweburl +"/_api/SP.AppContextSite(@target)/web/webs?@target='" +hostweburl + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler
        }
    );
}

function successHandler(data) {
    var jsonObject = JSON.parse(data.body);
    var results = jsonObject.d.results;
    for (var i = 0; i < results.length; i++) {
        document.getElementById("Error").innerHTML = results[i].Title;  
    }
}

1 Answer 1

1

Change below code

function successHandler(data) {
    var jsonObject = JSON.parse(data.body);
    var results = jsonObject.d.results;
    var result = '';
    for (var i = 0; i < results.length; i++) {
        result = result + results[i].Title + ' ';  
    }
    document.getElementById("Error").innerHTML = result;
}
7
  • Alright but then it also displays my site collection title i only wont to display the subsites titles?
    – Batistuta
    Commented Jan 9, 2015 at 13:15
  • Its the root site. Every sitecollection has a default root site. If you are not using it then delete the root site. Commented Jan 9, 2015 at 13:16
  • Yes i know that its the root site but i only want subsites, how could i delete it?
    – Batistuta
    Commented Jan 9, 2015 at 13:17
  • you can't have subsites without a root site collection, it is not possible to delete the root site, if you do that you delete everything in the collection Commented Jan 9, 2015 at 13:18
  • You can put an if condition and skip that result in the for loop. Commented Jan 9, 2015 at 13:18

Your Answer

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

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