Sharepoint - How do we create Sub Site using JavaScript object module

For JSOM Something like this would work...however you need to request full trust for your app.

function createWebsite() {

var clientContext = new SP.ClientContext('your url');
this.oWebsite = clientContext.get_web();

var webCreationInfo = new SP.WebCreationInformation();
webCreationInfo.set_title('My New Web Site');
webCreationInfo.set_description('Description of new Web site...');
webCreationInfo.set_language(1033);
webCreationInfo.set_url('MyNewWebSite');
webCreationInfo.set_useSamePermissionsAsParentSite(true);
webCreationInfo.set_webTemplate('mall');

oWebsite.get_webs().add(webCreationInfo);
oWebsite.update();

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

You can load the JS files in the default.aspx page like this in the PlaceholderMain.

<SharePoint:ScriptLink ID="ScriptLink5" name="sp.js" runat="server" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink ID="ScriptLink6" name="sp.runtime.js" runat="server" LoadAfterUI="true" Localizable="false" />
<SharePoint:ScriptLink ID="ScriptLink7" name="sp.core.js" runat="server" LoadAfterUI="true" Localizable="false" />

and i tend to add the ajax in the head.

 <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>

S


You can do it with REST API but be careful with user permission.

    $.ajax({
        url: "/_api/web/webinfos/add",
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        data: JSON.stringify({
            'parameters': {
                '__metadata': {
                    'type': 'SP.WebInfoCreationInformation'
                },
                'Url': <New SubSite URL>,
                'Title': <New SubSite Title>,
                'Description': 'Test',
                'Language': 1033,
                'WebTemplate': 'sts',
                'UseUniquePermissions': true
            }
        })           
    });

Tags: