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

%3CLINGO-SUB%20id%3D%22lingo-sub-1033215%22%20slang%3D%22en-US%22%3EUsing%20the%20REST%20API%20to%20export%20page%20layout%20and%20web%20parts%20to%20site%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1033215%22%20slang%3D%22en-US%22%3E%3CP%3EI'm%20currently%20using%20powershell%20command%20%22Get-SPOSiteScriptFromWeb%22%20to%20extract%20site%20scripts%20from%20existing%20sites.%20The%20plan%20is%20to%20use%20these%20scripts%20when%20provisioning%20new%20sites%20using%20the%20REST%20API%2C%20so%20that%20new%20sites%20get%20a%20predefined%20setup.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20understanding%20is%20that%20it's%20no%20longer%20supported%2Frecommended%20to%20export%20sites%20as%20templates%2C%20and%20that%20the%20recommended%20approach%20is%20to%20use%20site%20scripts%20to%20get%20the%20newly%20created%20sites%20set%20up%20with%20all%20the%20necessary%20web%20parts%20and%20so%20on.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20generated%20site%20script%20doesn't%20include%20information%20about%20web%20components%20and%20layout.%20Is%20it%20possible%20to%20get%20this%20information%20elsewhere%3F%20Or%20are%20there%20may%20other%20ways%20to%20create%20new%20sites%20basically%20as%20clones%20of%20existing%20sites%2C%20using%20the%20REST%20API%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBy%20the%20way%2C%20I%20posted%20this%20question%20over%20at%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fsharepoint.stackexchange.com%2Fquestions%2F272477%2Fexport-page-layout-and-web-parts-to-site-script%2F272645%23272645%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Estackexchange.com%3C%2FA%3E%26nbsp%3B%2C%20but%20the%20feedback%20so%20far%20revolves%20around%20using%20site%20templates%2C%20which%20as%20mentioned%20above%20doesn't%20seem%20to%20be%20the%20recommended%20approach%2C%20so%20I%20though%20I'd%20ask%20the%20same%20questions%20here%20as%20well.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1033988%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20the%20REST%20API%20to%20export%20page%20layout%20and%20web%20parts%20to%20site%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1033988%22%20slang%3D%22en-US%22%3E%3CP%3EHey%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F423531%22%20target%3D%22_blank%22%3E%40kenneho%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20suggest%20you%20to%20use%20the%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP-PowerShell%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EPnP%20Powershell%3C%2FA%3E%26nbsp%3Bto%20create%20site%20templates.%20I'd%20say%20that's%20the%20recomended%20approach%2C%20as%20it's%20been%20monthly%20updated%20by%20Microsoft%20and%20it's%20open%20source.%20You%20can%20use%20the%20command%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%2Fget-pnpprovisioningtemplate%3Fview%3Dsharepoint-ps%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EGet-PnPProvisioningTemplate%3C%2FA%3E%20to%20generate%20a%20file(preferrably%20xml)%20containing%20the%20site%20template.%20From%20there%20you%20can%20use%20the%20command%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%2Fapply-pnpprovisioningtemplate%3Fview%3Dsharepoint-ps%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EApply-PnPProvisioningTemplate%3C%2FA%3E%20to%20apply%20the%20generated%20template%20to%20another%20site.%20Although%20this%20feature%20is%20pretty%20reliable%2C%20you%20may%20run%20into%20some%20issues%20trying%20to%20%22duplicate%22%20the%20site%20without%20modifying%20a%20few%20lines%20of%20code(that%20was%20my%20experience%2C%20at%20least).%20But%20in%20the%20long%20run%20I'd%20say%20it%20is%20worth%20it.%20Let%20me%20know%20if%20you%20need%20any%20help.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1035311%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20the%20REST%20API%20to%20export%20page%20layout%20and%20web%20parts%20to%20site%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1035311%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20for%20your%20input%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F365643%22%20target%3D%22_blank%22%3E%40Carlos_Marins%3C%2FA%3E.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20different%20reasons%20I'm%20using%20Python%20instead%20of%20Powershell%2C%20so%20I%20need%20to%20find%20a%20way%20to%20convert%20your%20proposed%20solution%20over%20to%20Python.%20I'm%20thinking%20that%20the%20PnP%20Powershell%20module%20likely%20is%20talking%20to%20the%20Sharepoint%20Online's%20REST%20or%20Graph%20endpoint%2C%20so%20if%20I%20can%20figure%20out%20which%20endpoints%20there%20are%2C%20I%20can%20have%20my%20Python%20code%20use%20those%20endpoints%20as%20well.%20You%20wouldn't%20happen%20to%20have%20an%20idea%20which%20REST%20API%20endpoint%20may%20be%20involved%20here%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBy%20the%20way%2C%20I%20just%20came%20across%20this%20information%20from%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fsupport.office.com%2Fen-us%2Farticle%2Fcreate-and-use-site-templates-in-sharepoint-server-versions-60371b0f-00e0-4c49-a844-34759ebdd989%3Fui%3Den-US%26amp%3Brs%3Den-US%26amp%3Bad%3DUS%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fsupport.office.com%2Fen-us%2Farticle%2Fcreate-and-use-site-templates-in-sharepoint-server-versions-60371b0f-00e0-4c49-a844-34759ebdd989%3Fui%3Den-US%26amp%3Brs%3Den-US%26amp%3Bad%3DUS%3C%2FA%3E%3A%20%22%3CSPAN%3EThe%20Save%20site%20as%20template%20option%20is%26nbsp%3B%3C%2FSPAN%3Enot%20supported%26nbsp%3Bin%20SharePoint%20Server%202019%26nbsp%3B(modern%20sites)%26nbsp%3Bor%20SharePoint%20Online%26nbsp%3B(modern%20sites)%22.%20As%20I'm%20using%20SharePoint%20Online%20to%20create%20modern%20sites%2C%20maybe%20working%20with%20site%20templates%20is%20a%20lost%20case%20from%20the%20get-go.%20Have%20you%20succeeded%20in%20creating%20site%20templates%20from%20SharePoint%20Online%20modern%20sites%3F%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
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 stackexchange.com , 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 https://support.office.com/en-us/article/create-and-use-site-templates-in-sharepoint-server-versions...: "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.