Introduction

Here 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 text boxes where a user can put the site name, the site description and a submit button. On click, the site will get created.

HTML Code

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 successful message after the site is 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 the site we have hardcoded inside the __metadata.

REST API Code

The whole code we have added inside a script editor web part inside a web part page. 

 
<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 save the page it 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 is created successfully.


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



References:


If you want to know about Microsoft Flow then you can read below two articles:

Conclusion

Here we have discussed how we can create a site using REST API in SharePoint Online.