Create, Retrieve, Update Or Delete Sub Sites On SharePoint Using JavaScript Object Model

Introduction

In this article, you will learn how to retrieve, create, update or delete the sites under a site collection, using JavaScript Object Model (JSOM) on SharePoint 2013 / SharePoint Online sites like O365. The sites are considered as the sub sites within the site collection.

Before executing any operation, load the required scripts like SP.js, SP.RunTime.js. If the scripts are not initialized, use the snippet, given below:

  1. var scriptbaseURL = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";    
  2. $.getScript(scriptbaseURL + "SP.Runtime.js",    
  3.     function ()  
  4.    {    
  5.         $.getScript(scriptbaseURL + "SP.js", RequiredFunctions);    
  6.     }    
  7. );   

Create Sub Site

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

  • Access the context of the site and the Web object, using root site URL or the current context.
  • Create WebCreationInformation Object. Set the necessary parameters like title, description, URL, Web template, site permission and the language.
  • Add it to the existing Web collections and update the current site. Execute the request.

The code snippet, given below, helps in creating a site.

  1. var rootSiteUrl = "https://abc.sharepoint.com";  
  2. var ctx = new SP.ClientContext(rootSiteUrl);  
  3. var web = ctx.get_web();  
  4. var webInfo = new SP.WebCreationInformation();  
  5. webInfo.set_title('subsite');  
  6. webInfo.set_description('subsite description');  
  7. webInfo.set_url('subsite');  
  8. webInfo.set_webTemplate('STS#0');  
  9. webInfo.set_useSamePermissionsAsParentSite(true);  
  10. webInfo.set_language(1033);  
  11.   
  12. web.get_webs().add(webInfo);  
  13. web.update();  
  14. ctx.executeQueryAsync(function(){  
  15.     console.log("Sub Site Created");  
  16. },  
  17. function(sender, args){  
  18.     console.log("Failed to create sub site : " + args.get_message());  
  19. }  
  20. );   
Retrieve Sub Site

The site properties can be retrieved, using JSOM. 

  • Access the context and the Web object, using sub site URL.
  • The basic properties can be retrieved from the above Web object.
  • Other properties and information can be retrieved by accessing the necessary properties and methods. For example, just loading the Web object will not be able to retrieve more information on the features. To get the active features on the site, we need to access the feature on the select and load the object to retrieve the features information. Similarly, other collection values need to be retrieved by explicitly accessing the methods. 

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

  1. var subSiteUrl = "https://abc.sharepoint.com/subsite";  
  2. var ctx = new SP.ClientContext(subSiteUrl);  
  3. var web = ctx.get_web();  
  4. var features = web.get_features();  
  5. // Similarly other properties and information can be retrieved from the site  
  6. ctx.load(web);  
  7. ctx.load(features);  
  8. ctx.executeQueryAsync(function(){  
  9.     console.log("Title : " + web.get_title());  
  10.     console.log("Description : " + web.get_description());  
  11.     console.log("Totally " + features.get_count() + " Features active on the site")  
  12.     // Similarly other feature properties can be retrieved.  
  13. },  
  14. function(sender, args){  
  15.     console.log("Failed to retrieve sub site info : " + args.get_message());  
  16. }  
  17. );   

Update Sub Site

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

  • Access the context of the site and the Web object, using sub site URL.
  • Update the necessary properties, using the set methods.
  • Update and execute the request. 

The code snippet, given below, helps in updating the site properties. Here, title and the description of the sub site are updated.

  1. var subSiteUrl = "https://abc.sharepoint.com/subsite";  
  2. var ctx = new SP.ClientContext(subSiteUrl);  
  3. var web = ctx.get_web();      
  4. web.set_title('new subsite');   
  5. web.set_description('subsite description updated');  
  6. web.update();  
  7. ctx.executeQueryAsync(function(){  
  8.     console.log("Sub site properties updated");  
  9.     console.log("Title : " + web.get_title());  
  10.     console.log("Description : " + web.get_description());  
  11.  },  
  12.  function(sender, args){  
  13.     console.log("Failed to create sub site : " + args.get_message());  
  14.  }  
  15. );   

Delete Sub Site

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

  • Access the context of the site and the Web object, using sub site URL.
  • Using the Web object, delete the site with delete object method.
  • Execute the request, using the client context. 

The code snippet, given below, helps to delete the site.

  1. var subSiteUrl = "https://abc.sharepoint.com/subsite";  
  2. var ctx = new SP.ClientContext(subSiteUrl);  
  3. var web = ctx.get_web();      
  4. web.deleteObject();  
  5. ctx.executeQueryAsync(function(){  
  6.     console.log("Sub site deleted");          
  7. },  
  8. function(sender, args){  
  9.     console.log("Failed to create sub site : " + args.get_message());  
  10. }  
  11. );  

Note 

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.  

Summary

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