Created
February 12, 2017 19:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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