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.
- <script type="text/javascript" src="/SiteAssets/Scripts/jquery-1.9.1.min.js"></script>
- <script type="text/javascript">
- var dataval;
- $.ajax({
- url: _spPageContextInfo.webAbsoluteUrl+"/_api/web/webs?$orderby=created desc&$top=1",
- method: "GET",
- headers: { "Accept": "application/json; odata=verbose" },
- success: function (data) {
-
- console.log("Website '" + data.d.results[0].Title + "' created on " + data.d.results[0].Created)
- alert("Website '" + data.d.results[0].Title + "' created on " + data.d.results[0].Created);
- }
- });
- </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.
-
- $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
-
-
-
- function execCrossDomainRequest() {
-
-
- var executor = new SP.RequestExecutor(appweburl);
-
-
-
-
-
- executor.executeAsync(
- {
- url: appweburl + "/_api/SP.AppContextSite(@target)/web/webs?$orderby=created desc&$top=1&@target='" + hostweburl + "'",
- method: "GET",
- headers: {
- "Accept": "application/json; odata=verbose"
- },
- success: successHandler,
- error: errorHandler
- }
- );
- }
-
-
-
- function successHandler(data) {
- var jsonObject = JSON.parse(data.body)
-
- console.log("Website '" + jsonObject.d.results[0].Title+"' created on "+ jsonObject.d.results[0].Created);
- $('#message').html("Website '" + jsonObject.d.results[0].Title+"' created on "+ jsonObject.d.results[0].Created);
-
-
- }
-
- function errorHandler(data, errorCode, errorMessage) {
- console.log("Could not complete cross-domain call: " + errorMessage);
- }