Get Latest Child Site Created under SharePoint Site using REST API

Syntax

REST API Endpoint:

https://SharePointSiteURL/_api/web/webs?$orderby=created desc&$top=1

REST API endpoint to use in Add-ins:

<appweburl>/_api/SP.AppContextSite(@target)/web/webs?$orderby=created desc&$top=1&@target=<hostweburl>

Embed Code Snippet

The following code snippet can be added to SharePoint page or in content editor web part as a script. This example used to get the last created child site’s title.

  1. <script type="text/javascript" src="/SiteAssets/Scripts/jquery-1.9.1.min.js"></script>  
  2. <script type="text/javascript">  
  3. var dataval;  
  4. $.ajax({    
  5.    url: _spPageContextInfo.webAbsoluteUrl+"/_api/web/webs?$orderby=created desc&$top=1"//THE ENDPOINT    
  6.    method: "GET",     
  7.    headers: { "Accept""application/json; odata=verbose" },    
  8.    success: function (data) {   
  9.   
  10.     console.log("Website '" + data.d.results[0].Title + "' created on " + data.d.results[0].Created) //RESULTS HERE!!      
  11.     alert("Website '" + data.d.results[0].Title + "' created on " + data.d.results[0].Created);               
  12.    }    
  13. });  
  14. </script>  

Add-in Code Snippet

The following code snippet is used in SharePoint Add-in to get the latest child sub-site created under the SharePoint site.

  1. // Load the js files and continue to the successHandler  
  2. $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);  
  3.   
  4. // Function to prepare and issue the request to get  
  5. //  SharePoint data  
  6. function execCrossDomainRequest() {  
  7.       
  8.     // Initialize the RequestExecutor with the add-in web URL.  
  9.     var executor = new SP.RequestExecutor(appweburl);  
  10.   
  11.     // Issue the call against the add-in web.  
  12.     // To get the AllowRssFeeds property using REST we can hit the endpoint:  
  13.     //      appweburl/_api/web?select=AllowRssFeeds&@target=hostweburl  
  14.     // The response formats the data in the JSON format.      
  15.     executor.executeAsync(  
  16.         {  
  17.             url: appweburl + "/_api/SP.AppContextSite(@target)/web/webs?$orderby=created desc&$top=1&@target='" + hostweburl + "'",  
  18.             method: "GET",  
  19.             headers: {  
  20.                 "Accept""application/json; odata=verbose"  
  21.             },  
  22.             success: successHandler,  
  23.             error: errorHandler  
  24.         }  
  25.     );  
  26. }  
  27.   
  28. // Function to handle the success event.    
  29. // Prints the host web's title to the page.    
  30. function successHandler(data) {  
  31.     var jsonObject = JSON.parse(data.body)    
  32.     //jsonObject.d.results returns the collection web object properties     
  33.     console.log("Website '" + jsonObject.d.results[0].Title+"' created on "+ jsonObject.d.results[0].Created);            
  34.      $('#message').html("Website '" + jsonObject.d.results[0].Title+"' created on "+ jsonObject.d.results[0].Created);    
  35.       
  36.     
  37. }    
  38.     
  39. function errorHandler(data, errorCode, errorMessage) {    
  40.     console.log("Could not complete cross-domain call: " + errorMessage);    
  41. }