Create SharePoint Subsite using JavaScript

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.

We will also see, how to get all SharePoint subsites using the JavaScript object model (jsom).

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 on 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.

<h1>Create Site</h1>

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

<script type="text/javascript">

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();
var webTemplates = web.getAvailableWebTemplates(1033,false);
var enumerator = webTemplates.getEnumerator();
var customTemplate;
var webTemplate = enumerator.get_current();
var webTitle = webTemplate.get_title();
if(webTitle == templateTitle)
customTemplate = webTemplate.get_name();
var webCreationInformation = new SP.WebCreationInformation();
var newWeb= web.get_webs().add(webCreationInformation);
alert(‘Website created successfully.’);
function(sender, args){
function(sender, args){

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.

Get all SharePoint subsites using JavaScript

Now, we will see how to get all SharePoint subsites using JavaScript object model (jsom).

To use the code, create a web part page and a content editor webpart or script editor web part and add the below code.

function RetriveSites()
currentcontext = new SP.ClientContext.get_current();
currentweb = currentcontext.get_web();
//this.subsites = currentweb.get_webs();
this.subsites = currentweb.getSubwebsForCurrentUser(null);
currentcontext.executeQueryAsync(Function.createDelegate(this, this.ExecuteOnSuccess),
Function.createDelegate(this, this.ExecuteOnFailure));

function ExecuteOnSuccess(sender, args) {
var subsites = ";
var enum1 = this.subsites.getEnumerator();
var el = document.getElementById(‘LKMSubWebs');
while (enum1.moveNext())
var Site = enum1.get_current();
subsites += ‘\n' + ‘<a class=subsitecls href='+ Site.get_serverRelativeUrl()+'>'+Site.get_title()+'</a> </br>'
//RootElement.append(‘<a class=subsitecls href='+ siteTitle+'>'+siteTitle+'</a> </br>');
el.innerHTML = subsites;

function ExecuteOnFailure(sender, args) {

<div id="LKMSubWebs"></div>

This will bind all the SharePoint subsites titles and will display them.

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.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

  • >