top of page

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. });  

The code snippet given below helps in creating a site. In this example, the form digest value is retrieved directly from the page control. (The operation given above is not required)

  1. var restAPIURL = "/_api/web/webinfos/add";  

  2. var newSiteData = JSON.stringify({ 'parameters': 

  3. {  

  4. '__metadata': {   

  5. 'type': 'SP.WebInfoCreationInformation'          

  6. },  

  7. 'Url': 'SubSite',   

  8. 'Description': 'Subsite created from REST API',  

  9. 'Title': 'SubSite',  

  10. 'Language': 1033,  

  11. 'WebTemplate': 'STS#0',  

  12. 'UseUniquePermissions': false    

  13. }   

  14. });  

  15. $.ajax  

  16. ({        

  17. url: restAPIURL,        

  18. type: "POST",        

  19. async: false,      

  20. data: newSiteData,        

  21. headers: {  

  22. "accept": "application/json;odata=verbose",  

  23. "content-type": "application/json;odata=verbose",  

  24. "X-RequestDigest": $('#__REQUESTDIGEST').val()      

  25. },       

  26. success: function(data){             

  27. console.log('site created');      

  28. },        

  29. error: function(data){           

  30. console.log('Error creating site');       }    

  31. });  

Retrieve Sub Site

The site properties can be retrieved, using REST API. The URL to access the properties is, http://subsiteurl/_api/web

This will get the basic properties of the site. To get more properties, we need to use select and expand parameters in the URL. For example, the URL given above will not fetch more information on the features. To get the active features on the site, we need to pass the feature on the select and expand parameters to retrieve the information. Similarly, other collection values needs to be retrieved with the select and expand parameters.

The code snippet given below helps to retrieve the site properties.

  1. //var restAPIURL = "/subsite/_api/web?$select=Features&$expand=Features/Id"

  2. var restAPIURL = "/subsite/_api/web"

  3. $.ajax  ({        

  4. url: restAPIURL,        

  5. type: "GET",      

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

  7. success: function(data){             

  8. console.log("Title : " + data.d.Title);           

  9. console.log("Description : " + data.d.Description);      

  10. },        

  11. error: function(data){          

  12.  console.log('Error getting info');       

  13. }    

  14. });  

Update Sub Site

The update operation will be very similar to creating an operation.

  • The URL for updating the operation will be, http://subsiteurl/_api/web

  • .The method or type will be POST.

  • The properties that need to be updated are sent, using JSON string.

  • In the header parameter, we need to set and send the content type for the data type, form digest value for the security validation and “Merge” method for the update operation.

The code snippet given below helps in updating the site properties. Here, just update the title of the sub site created in the above sections:

  1. var updateSiteData = JSON.stringify(

  2. {  

  3. '__metadata': {   

  4. 'type': 'SP.Web'    

  5. },  

  6. 'Description': 'Subsite updated from REST API',  

  7. 'Title': 'New SubSite',   

  8. });  

  9. restAPIURL = "/subsite/_api/web"

  10. $.ajax  ({        

  11. url: restAPIURL,        

  12. type: "POST",      

  13. async: false,      

  14. data: updateSiteData,      

  15. headers: {  

  16. "accept": "application/json;odata=verbose",  

  17. "content-type": "application/json;odata=verbose",  

  18. "X-RequestDigest": $('#__REQUESTDIGEST').val(),    

  19. "X-HTTP-Method": "MERGE"     

  20. },       

  21. success: function(data){             

  22. console.log('site updated');      

  23. },        

  24. error: function(data){          

  25.  console.log('Error updating site');       

  26. }    

  27. });  

Delete Sub Site

The delete operation will be very similar to create or update operations. Here, no input data is required.

  • The URL will be http://subsiteurl/_api/web.

  • The type or method will be POST.

  • In the header parameter, we need to set and send the content type for data type, form digest value for the security validation and “Delete” method for the delete operation.

The code snippet given below helps delete the site.

  1. var restAPIURL = "/subsite/_api/web"

  2. $.ajax  ({        

  3. url: restAPIURL,        

  4. type: "POST",      

  5. async: false,      

  6. headers: {  

  7. "accept": "application/json;odata=verbose",  

  8. "content-type": "application/json;odata=verbose",  

  9. "X-RequestDigest": $('#__REQUESTDIGEST').val(),    

  10. "X-HTTP-Method": "DELETE"     

  11. },       

  12. success: function(data){             

  13. console.log('site deleted');      

  14. },        

  15. error: function(data){           

  16. console.log('Error deleting site');       

  17. }    

  18. });  


Thus, you have learned how to create, retrieve, update or delete the sub sites, using REST API on SharePoint 2013 / SharePoint online sites.



bottom of page