Working with Sites in SharePoint online office 365 using JavaScript Object Model jsom

delete site using jsom sharepoint 2013

Here we will discuss how we can create and delete site using JavaScript Object Model (jsom) in SharePoint online office 365. The same jsom code works as it is inside SharePoint 2013 as well as SharePoint 2016.

Create Site using JSOM in SharePoint Online:

First, let us discuss how we can create a site using the JavaScript Object Model (jsom) in SharePoint. Here we have put 3 textboxes and one submit button. In those textboxes, the user will enter the site title, description and site template. Once the user puts that information and click on OK, it will create the site and it will show a successful message.

If you want to learn about Microsoft Flow, you can see below useful articles:
Microsoft Flow Example Save tweets that include the specific hashtag to a SharePoint list

Microsoft Flow Copy files from one SharePoint Online account or folder to another Office 365

– Microsoft Flow Send approval email when a new item is added demo

Here we will put the JSOM code as well as the HTML code inside a script editor web part which we will add inside a web part page.

HTML Code:
<div id=”CreateSite”>

<div>

<strong>Name of Site:</strong>

<br />

<input type=”text” id=”txtSiteTitle” />

</div>

<br />

<div>

<strong>Description of Site:</strong>

<br />

<textarea cols=”20″ id=”txtSiteDescription”></textarea>

</div>

<div>

<br />

<strong>Site Template:</strong>

<br />

<input type=”text” id=”txtSiteTemplate” />

</div>

<br />

<input type=”button” id=”btnSubmit” value=”Create Site” />

</div>

<div id=”divResults”></div>

JSOM Code:
Here we are calling the createSite() method in the button click event. To create a site we also need to provide the site url, which we can taking the from the title. We are getting the site url by removing the space from the site title textbox.

var siteUrl = siteTitle.replace(/\s/g, “”);

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>

<script>

$(function () {

bindButtonClick();

});

function bindButtonClick() {

$(“#btnSubmit”).on(“click”, function () {

createSite();

});

}

function createSite() {

var siteTitle = $(“#txtSiteTitle”).val();

var siteDesc = $(“#txtSiteDescription”).val();

var siteUrl = siteTitle.replace(/\s/g, “”);

var siteTemplate = $(“#txtSiteTemplate”).val();

var clientContext = new SP.ClientContext();

var collWeb = clientContext.get_web().get_webs();

var webCreationInfo = new SP.WebCreationInformation();

webCreationInfo.set_title(siteTitle);

webCreationInfo.set_description(siteDesc);

webCreationInfo.set_language(1033);

webCreationInfo.set_url(siteUrl);

webCreationInfo.set_useSamePermissionsAsParentSite(true);

webCreationInfo.set_webTemplate(siteTemplate);

var oNewWebsite = collWeb.add(webCreationInfo);

clientContext.executeQueryAsync(

Function.createDelegate(this, this.onQuerySucceeded),

Function.createDelegate(this, this.onQueryFailed)

);

}

function onQuerySucceeded() {

$(“#divResults”).html(“Site successfully created!”);

}

function onQueryFailed(sender, args) {

alert(‘Request failed. ‘ + args.get_message() +

‘\n’ + args.get_stackTrace());

}

</script>

Once you will save the page, you can see the site creation form will look like below. Here user can put site name, description and template like below. On successful creation it will display the message like below:

SharePoint 2013 create site using JavaScript Object Model jsom
SharePoint 2013 create site using JavaScript Object Model jsom

Now if you will go to the site contents and then under sub sites section, you can see the sub site got created successfully.

sharepoint 2013 create site using javascript object model
sharepoint 2013 create site using javascript object model

Delete Site using JSOM SharePoint Online:

Now we will see how we can delete a SharePoint site using JavaScript object model (jsom) in SharePoint online. Here we have added a textbox where user can put the site title in the textbox and user can click on submit button which will delete the site and on successful deletion it will display a successful message to the user.

HTML Code:
<div id=”DeleteSite”>

<div>

<strong>Site Name to delete:</strong>

<br />

<input type=”text” id=”txtSiteTitle” />

</div>

<br />

<input type=”button” id=”btnSubmit” value=”Delete Site” />

</div>

<div id=”divResults”></div>

JSOM Code:
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>

<script>

$(function () {

bindButtonClick();

});

function bindButtonClick() {

$(“#btnSubmit”).on(“click”, function () {

deleteSite();

});

}

function deleteSite() {

var siteTitle = $(“#txtSiteTitle”).val();

var siteTitleNoSpaces = siteTitle.replace(/\s/g, “”);

var siteUrl = _spPageContextInfo.webAbsoluteUrl + “/” + siteTitleNoSpaces;

var clientContext = new SP.ClientContext(siteUrl);

var oWebsite = clientContext.get_web();

oWebsite.deleteObject();

clientContext.executeQueryAsync(

Function.createDelegate(this, this.onQuerySucceeded),

Function.createDelegate(this, this.onQueryFailed)

);

}

function onQuerySucceeded() {

$(“#divResults”).html(“Site successfully deleted!”);

}

function onQueryFailed(sender, args) {

alert(‘Request failed. ‘ + args.get_message() +

‘\n’ + args.get_stackTrace());

}

</script>

Once you Save the page, you can see the page like below, where user can put the site title and you can see on successful deletion it is displaying a successful message.

delete site using jsom sharepoint 2013
delete site using jsom sharepoint 2013

Now if you will go and see the site contents page, in sub sites section you will not see the site, because it is already got deleted.

sharepoint 2013 delete site using javascript object model jsom
sharepoint 2013 delete site using javascript object model jsom

Here we have checked how we can create a site using jsom in SharePoint online as well as how we can delete a site using jsom in SharePoint Online.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →