Using the REST API to export page layout and web parts to site script

Brass Contributor

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.


My understanding is that it's no longer supported/recommended to export sites as templates, and that the recommended approach is to use site scripts to get the newly created sites set up with all the necessary web parts and so on. 


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?


By the way, I posted this question over at , but the feedback so far revolves around using site templates, which as mentioned above doesn't seem to be the recommended approach, so I though I'd ask the same questions here as well. 

3 Replies

Hey @kenneho ,


I would suggest you to use the PnP Powershell to create site templates. I'd say that's the recomended approach, as it's been monthly updated by Microsoft and it's open source. You can use the command Get-PnPProvisioningTemplate to generate a file(preferrably xml) containing the site template. From there you can use the command Apply-PnPProvisioningTemplate to apply the generated template to another site. Although this feature is pretty reliable, you may run into some issues trying to "duplicate" the site without modifying a few lines of code(that was my experience, at least). But in the long run I'd say it is worth it. Let me know if you need any help.

Thanks for your input, @Carlos_Marins


For different reasons I'm using Python instead of Powershell, so I need to find a way to convert your proposed solution over to Python. I'm thinking that the PnP Powershell module likely is talking to the Sharepoint Online's REST or Graph endpoint, so if I can figure out which endpoints there are, I can have my Python code use those endpoints as well. You wouldn't happen to have an idea which REST API endpoint may be involved here? 


By the way, I just came across this information from "The Save site as template option is not supported in SharePoint Server 2019 (modern sites) or SharePoint Online (modern sites)". As I'm using SharePoint Online to create modern sites, maybe working with site templates is a lost case from the get-go. Have you succeeded in creating site templates from SharePoint Online modern sites? 



Hey @kenneho,


Oh, I don't know if you can really do it using Python. The PnP PowerShell Module uses CSOM AFAIK so not all features available could be translated to REST, although I believe many of them are. Do you have an exact set of features that you need for your site? We can look up if they are available through REST if you do.


About the link with site templates, yeah i have seen that before. It was kind of useful for classic sites, but not for Modern ones.