Working With Sites In SharePoint 2016 Using REST API

SharePoint 2016 general availability was announced in the Future Of SharePoint conference in May 2016. The series that discusses the installation of SharePoint 2016 in Azure can be found at C# Corner from the below links.

In this article, we will see how to interact with Sites in SharePoint, using REST API. As an initial prerequisite, ensure that the user, who is running the below scripts, has Site Collection administrator privileges.

We will use REST to connect to the SharePoint for performing the Site operations.The Scope of the article involves the following operations, using REST:

  • Create a new SharePoint 2016 Site
  • Delete an existing SharePoint 2016 Site

Create a new SharePoint Web

We can create the SharePoint site by issuing a POST AJAX request. The REST URL endpoint used for the operation is:


We will be creating a key value pair of information which will be used to create the Sharepoint site. The site creation information property is as shown below. It will be send as json in the ‘data’ attribute of the ajax rest call.

  1. {  
  2.     'parameters': {  
  3.         '__metadata': {  
  4.             'type''SP.WebInfoCreationInformation'  
  5.         },  
  6.         'Url''NewSubWeb',  
  7.         'Title''SubWeb Created using REST',  
  8.         'Description''REST created web',  
  9.         'Language': 1033,  
  10.         'WebTemplate''sts',  
  11.         'UseUniquePermissions'false  
  12.     }  
  13. }  
Within the _metadata attribute we have to specify the value for ‘type’ which will specify what object is being created. In our case it is site, hence we will be specifying ‘SP.WebInfoCreationInformation’. Along with this we will be specifying other web properties as a key value pair. The header section will look like,
  1. headers: {  
  2.     "accept""application/json;odata=verbose",  
  3.     "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  4.     "content-Type""application/json;odata=verbose"  
  5. },  
Here accept attribute specifies the data type for the return value and content-type defines the data type for the data sent to the server. In POST request we have to send the X-RequestDigest value along with the request for form validation without which we will get validation error. To fullfill this we will be assigning the $("#__REQUESTDIGEST").val() which sets the value of the form digest control present within the page to X-RequestDigest key.

Output: The console output shows a custom successful site creation message as shown below:


We can navigate to the site to see the newly created web.


Full Code

The full code for creating the site using REST is as shown below,
  1. <script language="javascript" type="text/javascript" src="//"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         var newSiteUrl = "/_api/web/webinfos/add";  
  5.         createSite(newSiteUrl)  
  6.     });  
  8.     function createSite(newSiteUrl) {  
  9.         $.ajax({  
  10.             url: _spPageContextInfo.webAbsoluteUrl + newSiteUrl,  
  11.             type: "POST",  
  12.             data: JSON.stringify({  
  13.                 'parameters': {  
  14.                     '__metadata': {  
  15.                         'type''SP.WebInfoCreationInformation'  
  16.                     },  
  17.                     'Url''NewSubWeb',  
  18.                     'Title''SubWeb Created using REST',  
  19.                     'Description''REST created web',  
  20.                     'Language': 1033,  
  21.                     'WebTemplate''sts',  
  22.                     'UseUniquePermissions'false  
  23.                 }  
  24.             }),  
  25.             headers: {  
  26.                 "accept""application/json; odata=verbose",  
  27.                 "content-type""application/json;odata=verbose",  
  28.                 "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  29.             },  
  30.             success: function(data) {  
  31.                 console.log(data);  
  32.                 console.log("The site has been created successfully.");  
  33.             },  
  34.             error: function(error) {  
  35.                 alert(JSON.stringify(error));  
  36.             }  
  37.         });  
  38.     }  
  39. </script>  
Delete exiting web

In order to delete the SharePoint site we can issue a DELETE AJAX request using the below REST endpoint.


In the header section we can specify the ‘accept’ and ‘X-RequestDigest’ attribute which specifies the return data type and the form digest value respectively. The “IF-Match” attribute is used to check concurrency of the site to ensure that the site that is being deleted is really the one that we intent to delete. We can either specify “*” which will blindly skip the concurrency check else we can specify the e-tag value (which can be obtained by issuing a GET request).

Output: On running the script for site deletion, a success message has come up in the console as shown below,


Full Code: The full code for deleting the site using REST is as shown below,
  1. <script language="javascript" type="text/javascript" src="//"></script>  
  2. <script language="javascript" type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         var existingSiteUrl = "/NewSubWeb/_api/Web";  
  5.         deleteSite(existingSiteUrl)  
  6.     });  
  8.     function deleteSite(existingSiteUrl) {  
  9.         $.ajax({  
  10.             url: _spPageContextInfo.webAbsoluteUrl + existingSiteUrl,  
  11.             type: "DELETE",  
  12.             headers: {  
  13.                 "accept""application/json;odata=verbose",  
  14.                 "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  15.                 "IF-MATCH""*"  
  16.             },  
  17.             success: function(data) {  
  18.                 console.log(data);  
  19.                 console.log("The web has been successfully deleted.");  
  20.             },  
  21.             error: function(error) {  
  22.                 alert(JSON.stringify(error));  
  23.             }  
  24.         });  
  25.     }  
  26. </script>  
Let’s see how to implement the above scripts in SharePoint. Below steps will demo how to work with the script for deleting the site. Similarly other scripts can also be tested. Save the script for deleting the site as a text file and upload it to Site Assets library.

SharePoint Implementation 
  • Go to the edit settings of the SharePoint page and click on Web part from the Insert tab

    Web part

  • Add Content Editor Web part,

    Add Content Editor

  • Click on Edit Web art from Content Edit Web part. Assign the URL of the script text file and click on Apply.

    Add Content Editor

  • Click on Apply and we can see the successful list deletion message from the console.

I had the Site page opened in the browser prior to running the script. Refreshing it gives the below error stating the deletion of the site.

Site page


Thus, we have seen how to create a SharePoint 2016 web and delete the existing web in SharePoint 2016, using REST API. This works the same way with Office 365 as well.