Kwizcom Forms

Create SharePoint Sub Site using jQuery JavaScript Client Object Model (jsom) in SharePoint 2016/2013/Online

create site using jsom sharepoint 2016

Here we will discuss how we can create a SharePoint subsite 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 in a web part page in SharePoint.

Create SharePoint Sub Site using JSOM

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

SharePoint deveopment training course
<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 SharePoint Sub Site using JSOM
Create SharePoint Sub Site using JSOM

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

You may like following SharePoint JSOM tutorials:

Hope this SharePoint tutorial helps to Create SharePoint Sub Site using jQuery JavaScript Client Object Model in SharePoint 2016/2013/Online.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope