Sunday 7 April 2013

A subsite cannot be created inside an app web

For all those who have started exploring SharePoint 2013 must be by now aware of the term App Web. Recently , I did a small POC to see how we can provision a subsite inside an app web. Now on first glance it seems to be a quite simple requirement as you are simply going to create a subsite. But this is not the case.
For App's things are a bit different. After some struggle to create a subsite inside an APP Web, I got an error message stating that "A subsite cannot be created inside an APP Web" .
For all those who wants to drill down further as to how this message came, please have a look at the below code snippet that gave the above result.



var hostweburl,appweburl;
jQuery(document).ready(function () {
    hostweburl = decodeURIComponent(
                getQueryStringParameter("SPHostUrl")
        );
    appweburl =
        decodeURIComponent(
            getQueryStringParameter("SPAppWebUrl")
    );

    
    jQuery("#btnSubmit").click(function () {
 
        createSite();
        
    });

});

function getQueryStringParameter(paramToRetrieve) {
    var params =
        document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve)
            return singleParam[1];
    }
}

function createSite() {

        context = new SP.ClientContext(appweburl);
        currentWeb = context.get_web();

        context.load(currentWeb);
                   context.executeQueryAsync(onCreationSuccess, OnFailure);
    }

    function onCreationSuccess() {

        var appInstanceID = currentWeb.get_appInstanceId();

        var webCreateInfo = new SP.WebCreationInformation();
        webCreateInfo.set_description("This site created from ECMA script");
        webCreateInfo.set_language(1033);
        webCreateInfo.set_title("Test");
        webCreateInfo.set_url("Test");
        webCreateInfo.set_useSamePermissionsAsParentSite(true);
        webCreateInfo.set_webTemplate("APP#0");

       web = currentWeb.get_webs().add(webCreateInfo,false,appInstanceID);

        context.load(web);
        context.executeQueryAsync(onSuccess, OnFailure);
        
    }

    function onSuccess() {
        alert("Web created");
    }

    function OnFailure(sender, args) {
        alert(args.get_message());
    }

1 comment: