Create SharePoint Site using Rest API in SharePoint Online Office 365

create sharepoint online site using rest api
InfoPath alternatives for form designing SharePoint

In this post we will discuss how 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 user can put the site name and the site description and a submit button. On click of that the site will got created.

Also you can read my previous posts on:
– Microsoft Flow Example Save tweets that include specific hashtag to a SharePoint list

– Configure On Premise environment for Apps in SharePoint 2016

– Start a task process workflow action Nintex workflow for office 365 SharePoint Online

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 site, we have hard coded inside the __metadata.

The whole code we have added inside a Script editor web part inside 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 () {

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 online site using rest api
create sharepoint online site using rest api

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

Create SharePoint site using rest api

Create SharePoint site using rest apiHope this will be helpful.

Similar SharePoint 2013 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 →

Leave a Reply