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