0

I'm currently using powershell command "Get-SPOSiteScriptFromWeb" to extract site scripts from existing sites. The plan is to use these scripts when provisioning new sites using the REST API, so that new sites get a predefined setup.

The generated site script doesn't include information about web components and layout. Is it possible to get this information elsewhere? Or are there may other ways to create new sites basically as clones of existing sites, using the REST API?

  • can you take site template and apply it to your new site using REST API? – Karthik Jaganathan Nov 21 '19 at 13:36
  • @KarthikJaganathan, using site templates would be great, but I've yet to find the relevant REST API endpoints for creating new sites based on site templates. Would you mind pointing me to the relevant endpoints? – kenneho Nov 26 '19 at 6:57
1

You can try the below code to create subsite using JSOM,

1. CreateSubsiteByTemplateName(title, description, webUrl, templateTitle)

function CreateSubsiteByTemplateName(title, description, webUrl, templateTitle) {   
   var context = new SP.ClientContext.get_current();   
   var web = context.get_web();   
   context.load(web);   
   var webTemplates = web.getAvailableWebTemplates(1033, false);   
   context.load(webTemplates);   
   context.executeQueryAsync(function () {   
    var enumerator = webTemplates.getEnumerator();   
    var templateId = "STS#0";   
    while (enumerator.moveNext()) {   
     var webTemplate = enumerator.get_current();   
     var webTitle = webTemplate.get_title();   
     if (webTitle == templateTitle) {   
      templateId = webTemplate.get_name();  
      break;   
     }   
    }   
    CreateSubsiteByTemplateId(title, description, webUrl, templateId);   
   },   
    function (sender, args) {   
     alert(args.get_message())   
    }   
   );   
  }  

2. function CreateSubsiteByTemplateId(title, description, webUrl, templateId)

function CreateSubsiteByTemplateId(title, description, webUrl, templateId) {    
   var restAPIURL = "/_api/web/webinfos/add";    
   var newSiteData = JSON.stringify(    
   {    
   'parameters': {    
    '__metadata': {    
    'type': 'SP.WebInfoCreationInformation'    
    },    
    'Url': webUrl,    
    'Description': 'Subsite created from REST API',    
    'Title': title,    
    'Language': 1033,    
    'WebTemplate': templateId,    
    'UseUniquePermissions': true    
   }    
   });    
   $.ajax    
   ({    
   url: restAPIURL,    
   type: "POST",    
   async: false,    
   data: newSiteData,    
   headers: {    
    "accept": "application/json;odata=verbose",    
    "content-type": "application/json;odata=verbose",    
    "X-RequestDigest": $('#__REQUESTDIGEST').val()    
   },    
   success: function (data) {    
    console.log('site created');    
   },    
   error: function (data) {    
    console.log('Error creating site');    
   }    
   });    
  }    

Source

For more informations, kindly refer the below links,

  1. Apply Site Template in a SharePoint Site 2013 Using REST API

  2. Manage modern SharePoint sites using REST

| improve this answer | |
  • Thanks! After reading up on the subject, I get the impression that creating custom site templates isn't really supported anymore, but one can use the https://tenant.sharepoint.com/sites/mysite/_layouts/15/savetmpl.aspx workaround to get it working. First of all, this doesn't work for me, and second I prefer to go for a solution that's not likely to be deprecated any time soon. So I'm basically back to my original query - is there a way to use the REST API to download the site script for a site? – kenneho Nov 26 '19 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.