Create, Retrieve, Update or Delete Sub Sites On SharePoint Using REST API


In this article, you will learn how to retrieve, create, update or delete the sites under a site collection, using REST API's on SharePoint 2013 / SharePoint Online sites like O365. The sites are considered as sub sites within the site collection.

Steps Involved

  • Identify the site URL and build the REST API URL.

  • Use AJAX jQuery call to accomplish the necessary operation.

  • You can set the return value type in the AJAX call request. In my case, I have specified JSON to be the return value in the data parameter.

  • In these cases, you can check the result values in a Browser debugger console. You can change the logic to display the results on the pages.

  • Place these scripts on the page, using the Web parts (Content Editor / Script Editor / any custom Web part) at the site collection level.

Create Sub Site

The operation will create a sub site within the site collection. 

  • The REST API will complete the create operation http://siteurl/_api/contextinfo.

  • The method or type should be POST. 

  • The form digest value is required to validate the create operation. Hence, before creating the request, the form digest value should be retrieved. We can use an AJAX call to accomplish all the tasks. The REST API will retrieve form digest value  http://siteurl/_api/contextinfo. Or simply from the request digest control, we can retrieve the form digest.value.$('#__REQUESTDIGEST').val().

  • For creating a site, we need to send the necessary information to the Service. For this, we will use JSON format, where we will send site title, site template, site URL, description, unique permissions etc.

  • In the header, send the required content type and form digest value for the security validation.

To get the form digest value, you can use the logic, given below:

  1. $.ajax     

  2.  ({           

  3.  url: "/_api/contextinfo",            

  4. type: "POST",            

  5. async: false,            

  6. headers: { "accept": "application/json;odata=verbose" },            

  7. success: function(data)

  8. {                

  9.  requestDigest = data.d.GetContextWebInformation.FormDigestValue;          

  10. },            

  11. error: function(data){            }        

  12. });