Create or Delete SharePoint Site using Rest API

This SharePoint Rest API tutorial we will discuss how to create a SharePoint site using Rest API. Also, we will check how to delete a SharePoint site using Rest API in SharePoint Online or SharePoint 2013/2016.

Create SharePoint Site using Rest API

We can create a SharePoint site using Rest API in SharePoint Online Office 365. This will also work fine with SharePoint 2013 and SharePoint 2016.

Here we have taken two textboxes where the user can put the SharePoint site name and the site description and a submit button. On click of that, the SharePoint site will get created.

The html code looks like below:

<div id="CreateSite">
<div>
<strong>Site Name:</strong>
<br />
<input type="text" id="txtSiteTitle" />
</div>
<br />
<div>
<strong>Site Description:</strong>
<br />
<textarea cols="20″ id="txtSiteDescription"></textarea>
</div>
<br />
<input type="button" id="btnSubmit" value="Create Site" />
</div>
<div id="divResults"></div>

Here divResults will display the successfull message after the site got created successfully.

Then the script code will be like below. Here in the createSite() method we are retrieving the site title and description from the textboxes and we are making the site URL by removing the space from the title. Other properties which were required to create a site, we have hardcoded inside the __metadata.

The whole code we have added inside a Script editor web part or in content editor web part, inside a web part page.

Rest API 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 () {
createSite();
});
}

function createSite() {
var newSiteTitle = $("#txtSiteTitle").val();
var newSiteDesc = $("#txtSiteDescription").val();
var newSiteUrl = newSiteTitle.replace(/\s/g, "");
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
var fullUrl = siteUrl + "/_api/web/webinfos/add";
$.ajax({
url: fullUrl,
type: "POST",
data: JSON.stringify({
'parameters': {
'__metadata': { 'type': 'SP.WebInfoCreationInformation' },
'Url': newSiteUrl,
'Title': newSiteTitle,
'Description': newSiteDesc,
'Language': 1033,
'WebTemplate': 'sts#0',
'UseUniquePermissions': false
}

}),

headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: onQuerySucceeded,
error: onQueryFailed
});
}

function onQuerySucceeded() {
$("#divResults").html("Site Created Successfully!");
}

function onQueryFailed(sender, args) {
alert('Error!');
}
</script>

Once you will Save the page and the page looks like below. Here put a title for the site and a description for the site. Then click on the Submit button. It will show a successful message once the site got created successfully.

create sharepoint site using rest api
create sharepoint site using rest api

You can navigate to the Site Contents page and you can see the site got created successfully.

create sharepoint online site using rest api
create sharepoint online site using rest api

Delete SharePoint Site using Rest API

Now we will see how we can delete a site using Rest API in SharePoint Online Office 365. The same code we can use to delete a site using Rest API in SharePoint 2013/2016.

Here in this example, we are going to take a textbox and a button. The user can put the site title in the textbox and can click on the button to delete the site. The HTML code looks like below and the div with id divResults will display the successful message when the site will get deleted successfully.

HTML Code:

<div id="DeleteSite">
<div>
<strong>Enter Name of Site to Delete:</strong>
<br />
<input type="text" id="txtSiteTitle" />
</div>
<br />
<input type="button" id="btnSubmit" value="Delete Site" />
</div>
<div id="divResults"></div>

Rest API Code:

Below is the Rest API code, here we have taken the site from the textbox. Let us added the full HTML and rest API code inside a script editor web part. You can also use a content editor web part. For this example let us put inside a script editor web part in a web part page.

<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;
var fullUrl = siteUrl + "/" + siteTitleNoSpaces + "/_api/web";
$.ajax({
url: fullUrl,
type: "POST",
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "DELETE",
"IF-MATCH": "*"
},

success: onQuerySucceeded,
error: onQueryFailed
});
}

function onQuerySucceeded() {
$("#divResults").html("Site deleted successfully !");
}

function onQueryFailed(sender, args) {
alert('Error!');
}

</script>

Once you save the page, the form will appear like below, where users can put the title and click on the Delete Site button. If the site will get deleted successfully, it will show a successful message like below:

delete sharepoint online site using rest api
delete sharepoint online site using rest api

Now if you will check the SharePoint Site Contents page, you can see the site will not be there.

delete sharepoint site using rest api
delete sharepoint site using rest api

You may like following SharePoint Rest API tutorials:

Hope this SharePoint tutorial explains how to create SharePoint site using rest api and how to delete SharePoint site using Rest API.

free sharepoint training

SharePoint Online FREE Training

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

envelope
envelope

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

>