SharePoint Online is a powerful tool for creating intranet sites, and it's even more powerful when you can create and use custom templates to streamline the process for multiple projects or clients. Unfortunately, creating a template from an existing SharePoint Online site can be a difficult task, especially for non-programmers.
In the past, creating a template was as simple as a few clicks in SharePoint classic, but with the new SharePoint Online, it's not so easy anymore. I've tried various methods, such as using flows and http requests, but I've found that the best way to clone a site multiple times is by using PowerShell to export the existing site and create a template from it.
I created a simple script based on Microsoft's documentation, and even corrected a typo in the documentation regarding spaces in list and library names. I even corrected a typo on the docs! The internal names, such as "Shared%20Documents" had to be used instead of "Shared Documents".
In the upcoming Part 2 of this post, I will review the entire workflow, from a SharePoint list for projects, to a Power Automate flow that creates the site, provides permissions, applies the template, copies over some template files, and even applies customizations to the home page.
In summary, publishing an existing site as a template using PowerShell is the best way to clone a site multiple times. It's a little technical, but with the right script and a little bit of tweaking, you can make this process a lot more manageable.
Before proceeding, make sure to install the SharePoint Online Management Shell. The script I've provided includes a check and installation for the module, but I recommend installing it beforehand.
Open PowerShell as administrator and try my sample code below. Adapting the sample script with some parameters and handling the module installation, we can use the following:
When I first started working with SharePoint Site Designs and Site Scripts, I hadn't used them before and wasn't familiar with their structure. However, I found a great resource in DevFacto's blog post on Getting Started with SharePoint Site Designs and Site Scripts. It helped me understand the anatomy of a site script, which was especially helpful when I needed to split my template file...
...
I initially didn't want to split the template file, but I learned that site scripts are limited to 100,000 characters. My site had multiple lists and libraries with lots of columns and formatting, so I realized that I had more work ahead of me. Excited for the challenge, I decided to split my file by list and library. This way, it would be more modular for future use and each list/library file could be used individually in a new template if needed. If your template is simple and the file is less than 100,000 characters, then you can skip the next section where I discuss splitting the file.
Reading DevFacto's post was particularly helpful as it gave me a better understanding of the structure of the site script. The post does a great job of outlining the basic structure of the script that I had to use for each file:
The structure of the script helped me to split my file by list and library which made it more modular for future use. My template site had 3 libraries and 7 lists, so I created 10 files. Each file had the same structure and looked something like this:Once we put all of that together, and understand the anatomy of a site script, we can clone very complex site templates.
If you take the original script from above and add a few more parameters to check if the script should import one or more files instead, you get the following: