Skip to content

Instantly share code, notes, and snippets.

@rheid
Created February 12, 2017 19:52
Show Gist options
  • Save rheid/4f4f321b789255b10ccbba1da17b9898 to your computer and use it in GitHub Desktop.
Save rheid/4f4f321b789255b10ccbba1da17b9898 to your computer and use it in GitHub Desktop.
SharePoint JS CSOM code to create a subsite using a custom Site Template
var webUrl = "newSubSite",
webTitle = "New Sub Site",
webDesc = "Description about the new web/site";
var siteTemplate;
var CONST_PROJECT_TEMPLATE = "Custom Project Template";
var ctx = SP.ClientContext.get_current();
var webTemplates = ctx.get_web().getAvailableWebTemplates(1033, false);
ctx.load(webTemplates);
ctx.executeQueryAsync(function () {
//Get Custom Site Template Name
var tmpls = webTemplates.getEnumerator();
while (tmpls.moveNext()) {
var tmpl = tmpls.get_current();
if (tmpl.get_title() == CONST_PROJECT_TEMPLATE) {
siteTemplate = tmpl.get_name();
}
}
//Create SPWeb
var webCreationInformation = new SP.WebCreationInformation();
webCreationInformation.set_title(webTitle);
webCreationInformation.set_description(webDesc);
webCreationInformation.set_language(1033);
webCreationInformation.set_url(webUrl);
//Set false, if Unique Permissions are required
webCreationInformation.set_useSamePermissionsAsParentSite(true);
webCreationInformation.set_webTemplate(siteTemplate);
var newSiteObj = ctx.get_web().get_webs().add(webCreationInformation);
ctx.load(newSiteObj, 'Title', 'Url');
ctx.executeQueryAsync(function () {
console.log("Site created. Title : " + newSiteObj.get_title() + ", Url : " + newSiteObj.get_url());
}, function (sender, args) {
console.error(args.get_message());
});
}, function (sender, args) {
console.error(args.get_message());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment