Create SharePoint Sub Site using jQuery JavaScript Client Object Model in SharePoint 2016

create site using jsom sharepoint 2016

Here we will discuss how we can create a SharePoint sub site using jQuery and JavaScript client object model in SharePoint 2016, SharePoint 2013 or SharePoint Online. The code will work in SharePoint 2016/2013 and SharePoint Online also. You can directly put the below code inside a script editor web part.


Read some SharePoint 2013 tutorials below:

As you can see here I am taking one textbox and I am using the value for site name, description as well as the url.



<h1>Create Site</h1>

Site Name: <input type=”text” name=”txtSitename” id=”txtSitename”>
<input type=”submit” value=”Submit” id=”btnCreateSite”>

<script type=”text/javascript”>

$(“#btnCreateSite”).click(function(){
CreateWebsite($(“#txtSitename”).val(), $(“#txtSitename”).val(), $(“#txtSitename”).val(), “BLANKINTERNET#2”);
});

function CreateWebsite(title, description, webUrl, templateTitle)
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
context.load(web);
var webTemplates = web.getAvailableWebTemplates(1033,false);
context.load(webTemplates);
context.executeQueryAsync(function(){
var enumerator = webTemplates.getEnumerator();
var customTemplate;
while(enumerator.moveNext())
{
var webTemplate = enumerator.get_current();
var webTitle = webTemplate.get_title();
if(webTitle == templateTitle)
{
customTemplate = webTemplate.get_name();
break;
}
}
var webCreationInformation = new SP.WebCreationInformation();
webCreationInformation.set_title(title);
webCreationInformation.set_description(description);
webCreationInformation.set_language(1033);
webCreationInformation.set_url(webUrl);
webCreationInformation.set_useSamePermissionsAsParentSite(true);
webCreationInformation.set_webTemplate(customTemplate);
var newWeb= web.get_webs().add(webCreationInformation);
context.executeQueryAsync(function(){
alert(‘Website created successfully.’);
},
function(sender, args){
alert(args.get_message());
});
},
function(sender, args){
alert(args.get_message())
}
);
}
</script>

The form will look like below:

create site using jsom sharepoint 2016
create site using jsom sharepoint 2016


Give a name and click on Submit button which will create a sub site under the site.

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials: