Automation Of Creating SharePoint Site Using Custom Site Template

SharePoint Online is being used by many organizations not only for collaboration and document management but it is used for several business activities which involve similar types of custom sites.

Let’s take an example project site. User creates a separate site to manage each of the project where the architecture, configuration, and design of the site are the same. SharePoint provides the ability to create a Site Template using an existing site using PnP PowerShell Script.

I am describing here the steps to create a SharePoint site automatically using the existing site template. In this article, we will be using SharePoint List, Power Automate, Azure Automation, and Azure Runbook.

First create a template file using the existing SharePoint Site using below PowerShell Script

$TemplateSiteUrl = "https://tenant.sharepoint.com/sites/templateSite"
$TemplateFile = "\TemplateFile.xml"
Connect - PnPOnline - Url $TemplateSiteUrl - PnPManagementShell
Get - PnPSiteTemplate - Out $SiteTemplateFile

The above script creates a XML file called TemplateFile.xml. Upload this file to a SharePoint Library somewhere so that we can use it each time by PowerShell to create a new site.

Now login to Azure Portal and create an Automation Account.

Once automation account is created, then go to automation account.

First you need to install the module you require to run the PowerShell.

Click the “Modules” under “Shared Resource” from the left panel and click the “Browse Gallery”

Automation of creating SharePoint Site using custom site template

Then search the pnp.powershell. This will show the module and select.

Automation of creating SharePoint Site using custom site template

When you installed the required modules, go to the Runbook.

You will see a Runbook under Process Automation in the left panel.

Automation of creating SharePoint Site using custom site template

Click the runbook and create a new runbook. Provide the name of runbook, select PowerShell as runbook type, runtime version, and description.

More details about the runbook https://learn.microsoft.com/en-us/azure/automation/manage-runbooks

Once runbook is created, go to runbook. You will find "Edit" link on top with other links.

Automation of creating SharePoint Site using custom site template

Click the edit link. You will find an editor to put your PowerShell Script. Now enter the below PowerShell Script. We will be passing SiteURL as parameter.

param(
    [parameter(Mandatory = $true)]
    [string] $SiteURL = "https://tenant.sharepoint.com/sites/SiteName")
$UserName = "[email protected]"
$Password = "xxxxxxxxx"
$SecurePassword = ConvertTo - SecureString - String $Password - AsPlainText - Force
$Cred = New - Object - TypeName System.Management.Automation.PSCredential - argumentlist $UserName, $SecurePassword
#Above line will connect SharePoint Site
Connect - PnPOnline - Url "https://tenant.sharepoint.com/sites/NewSiteName" - Credentials $Cred
$fileRelativeURL = "/sites/SiteName/shared Documents/TemplateFile.xml"
#Download template xml file.
$file = Get - PnPFile - Url $fileRelativeURL - AsFile - Path c: \temp - Filename " TemplateFile.xml"
# Connect new site where template will be implemented.
Connect - PnPOnline - Url $SiteURL - Credentials $Cred
#Implement the template.
Invoke - PnPSiteTemplate - Path "c:\temp\TemplateFile.xml" - ClearNavigation

After putting the script, click Save and Publish the runbook.

Now Create a SharePoint List that will contain a list of projects.

Then create a new flow using Power Automate connected to the above list triggered “When an item is created” so that flow trigger as soon as a new item is created in the list. Add next steps in the flow as “Send an HTTP request to SharePoint” and put the details as below:

Site Address: Your SharePoint site address
Method: POST
Uri: /_api/SPSiteManager / create
Headers: accept - application / json;
odata.metadata = none
Body: {
    "request": {
        "Title": "Site Name returned from list",
        "Url": "https://tenant.sharepoint.com/sites/SiteName",
        "Lcid": 1033,
        "ShareByEmailEnabled": false,
        "Description": "Description of site",
        "WebTemplate": "STS#0",
        "Owner": "[email protected]",
    }
}

Microsoft Power Automate has connector available to connect with a runbook under an automation account. Add an action and select "Azure Automation" connector, then select “Create a job” from the list of actions.

This will require your azure details like subscriptions, resource group, automation account, and runbook. When you select your runbook, it will prompt for parameter defined in the runbook. Pass the parameter and save. This will call the script and pass the parameter to script.

You can add more actions in the flow like updating a list or sending a notification mail etc.

Your flow will look like below.

Automation of creating SharePoint Site using custom site template

Now automation is ready.

Please create a new item to the list. It will trigger the flow and create a new site with custom template.