3

I am trying to get all sub sites of the top level site. However, the code I wrote for the app returns zero subsites. I am a little mystified as to why:

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
var web = context.get_web();
var lists = web.get_lists();

// this is used later to hold the sub webs found
var webCollection = null;

function getSubWebs(){
    webCollection = web.getSubwebsForCurrentUser(null);
    context.load(webCollection)
    context.executeQueryAsync(onGetSubwebsSuccess, onGetSubwebsFail);
    log("executing getSubWebs query");
}

function onGetSubwebsSuccess(sender, args){
    log("getSubWebs query successful")
    alert("Number of sites: " + webCollection.get_count());
    var selectSubwebsBox = $("#selectsubwebs");
    var webEnumerator = webCollection.getEnumerator();
    //clearSubWebList();

    while (webEnumerator.moveNext()){
        var web = webEnumerator.get_current();

        log("Adding " + web.get_title + " to the subwebs list.")
        selectSubwebsBox.append($("<option>", { value: web.get_title })).text(web.get_title);
    }
}

function onGetSubwebsFail(sender, args){
    log("getSubWebs query failed")
    alert("Request to retrieve subwebs failed. Error: " + args.get_message())
}

function clearSubWebList(){
    $("#selectsubwebs").find("option").remove();
    log("SubWebsList cleared");
}

function log(msg){
    // not all browser support console/logging
    if (window.console){
        window.console.log(msg);
    }
}

Why is the SharePoint Javascript OM methods returning zero subsites? Is this not the correct way to do this?

5

Your code is, for the most part, correct. However, since this is done in an app, what you're doing is getting all the subsites of the app, which are zero. You need to use the Cross-Domain library, since the host resides on a separate domain.

In order to call the Cross-Domain library, you just need to reference the SP.RequestExecutor.js file. You can either add this to your app, or use the following:

var scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js", function(){
    $.getScript(scriptbase + "SP.js", function(){
        $.getScript(scriptbase + "SP.RequestExecutor.js", getSubWebs); // You only need this line if you already reference the SP.Runtime.js/SP.js files
    });
});

Regardless of how you get the SP.RequestExecutor.js file, you'll have to make the following changes to your getSubWebs function:

function getSubWebs(){
    context = new SP.ClientContext(appweburl);
    var factory = SP.ProxyWebRequestExecutorFactory(appweburl);
    context.set_webRequestExecutorFactory(factory);
    var appContextSite = new SP.AppContextSite(context, hostweburl);

    web = appContextSite.get_web();
    webCollection = web.getSubwebsForCurrentUser(null);
    context.load(webCollection)
    context.executeQueryAsync(onGetSubwebsSuccess, onGetSubwebsFail);
    log("executing getSubWebs query");
}

Here is a working code sample from MSDN.

  • What are the permissions required to read the subsites list? – Mika A. May 13 '15 at 14:44
0

looks fine to me!

how about adding ExecuteOrDelayUntilScriptLoaded to the code?

http://msdn.microsoft.com/en-us/library/ff411788(v=office.14).aspx

so in your case add:

ExecuteOrDelayUntilScriptLoaded(getSubWebs, "sp.js"); 

so full code is:

var context = SP.ClientContext.get_current();
var user = context.get_web().get_currentUser();
var web = context.get_web();
var lists = web.get_lists();

// this is used later to hold the sub webs found
var webCollection = null;

ExecuteOrDelayUntilScriptLoaded(getSubWebs, "sp.js"); 

function getSubWebs(){
    webCollection = web.getSubwebsForCurrentUser(null);
    context.load(webCollection)
    context.executeQueryAsync(onGetSubwebsSuccess, onGetSubwebsFail);
    log("executing getSubWebs query");
}

function onGetSubwebsSuccess(sender, args){
    log("getSubWebs query successful")
    alert("Number of sites: " + webCollection.get_count());
    var selectSubwebsBox = $("#selectsubwebs");
    var webEnumerator = webCollection.getEnumerator();
    //clearSubWebList();

    while (webEnumerator.moveNext()){
        var web = webEnumerator.get_current();

        log("Adding " + web.get_title + " to the subwebs list.")
        selectSubwebsBox.append($("<option>", { value: web.get_title })).text(web.get_title);
    }
}

function onGetSubwebsFail(sender, args){
    log("getSubWebs query failed")
    alert("Request to retrieve subwebs failed. Error: " + args.get_message())
}

function clearSubWebList(){
    $("#selectsubwebs").find("option").remove();
    log("SubWebsList cleared");
}

function log(msg){
    // not all browser support console/logging
    if (window.console){
        window.console.log(msg);
    }
}

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.