Dynamic subsite creation using Azure Automation job and PNP PowerShell in SharePoint online classic site

This blog starts going back to couple of years, where our team was migrating SharePoint 2013 sites to SharePoint cloud.
Requirement:
In SharePoint 2013, sites can be created using “save site as template” option. However, when it comes to SharePoint online , the feature was deprecated. In 2013 “save site as template” feature allows to create site with just 5 to 10 clicks to create site using already saved site template with all customizations like custom navigation links, master page, libraries, site columns, views, and folders in library.
Azure being the savior for most of the use cases, my first thought was to bend towards Azure for performance and ease of deployment. Azure has an automation service to run scheduled PowerShell script through its runbooks to create subsite with custom changes.
Following are the steps that explains to schedule PowerShell script using Azure automation job and call automation job from Microsoft flow which in turn is triggered on button click in Canvas PowerApps.
PowerShell script for runbook:
Set input parameters
Param
(
[Parameter (Mandatory= $true)]
[String] $sitename,
[Parameter (Mandatory= $true)]
[String] $siteDescription
)
Create classic team site using credential object of automation job and connect to new subsite
Save credentials in automation instead of referring credentials in code to avoid security vulnerability.
try
{
$credObject = Get-AutomationPSCredential -Name "automationcredentialname"
#connect to root site and get webpart xml
Connect-PnPOnline (replace sitecollection URL) -cred $credObject
#Enable feature in site coll needed to avoid push error
Enable-PnPFeature -Identity 41e1d4bf-b1a2-47f7-ab80-d5d6cbba3092
#connect to project sub site for new site creation
Connect-PnPOnline (replace subsite URL) -cred $credObject
Write-Output "Connected to site collection"
$subsite=New-PnPWeb -Title $sitename -Url $sitename -Description $siteDescription -Locale 1033 -Template "STS#0"
Write-Output "Site created using team site template STS#0"
$siteurl = $subsite.Url
Write-Output "site url:" $siteurl
Connect-PnPOnline $siteurl -cred $credObject
Write-Output "Connected to newly created site"
Create custom libraries
#create libraries
New-PnPList -Title "Capture" -Template DocumentLibrary
New-PnPList -Title "Procurement Documents" -Template DocumentLibrary
New-PnPList -Title "Project Tools" -Template DocumentLibrary
New-PnPList -Title "Support Documents" -Template DocumentLibrary
New-PnPList -Title "Working Documents" -Template DocumentLibrary
New-PnPList -Title "Pink Team" -Template DocumentLibrary
New-PnPList -Title "Gold Team" -Template DocumentLibrary
New-PnPList -Title "Submitted" -Template DocumentLibrary
New-PnPList -Title "Post Submittal" -Template DocumentLibrary
New-PnPList -Title "DTP" -Template DocumentLibrary
New-PnPList -Title "Completed library" -Template DocumentLibrary
Write-Output "All lists created and content types attached"
Create custom navigation links to created libraries
# Delete all navigation from left
Get-PnPNavigationNode -Location QuickLaunch | Remove-PnPNavigationNode -Force
#Navigation to left
Add-PnPNavigationNode -Title "Capture" -Location "QuickLaunch" -Url "Capture/"
Add-PnPNavigationNode -Title "Completed library" -Location "QuickLaunch" -Url "Completed library/"
Add-PnPNavigationNode -Title "DTP" -Location "QuickLaunch" -Url "DTP/"
Add-PnPNavigationNode -Title "Gold Team" -Location "QuickLaunch" -Url "Gold Team/"
Add-PnPNavigationNode -Title "Pink Team" -Location "QuickLaunch" -Url "Pink Team/"
Add-PnPNavigationNode -Title "Post Submittal" -Location "QuickLaunch" -Url "Post Submittal/"
Add-PnPNavigationNode -Title "Project Tools" -Location "QuickLaunch" -Url "Project Tools/"
Add-PnPNavigationNode -Title "Procurement Documents" -Location "QuickLaunch" -Url "Procurement Documents/"
Add-PnPNavigationNode -Title "Submitted" -Location "QuickLaunch" -Url "Submitted/"
Add-PnPNavigationNode -Title "Support Documents" -Location "QuickLaunch" -Url "Support Documents/"
Add-PnPNavigationNode -Title "Working Documents" -Location "QuickLaunch" -Url "Working Documents/"
#disable structural navigation caching to show left nav else quick launch links are not shown
Set-PnPStructuralNavigationCacheWebState -IsEnabled $false
Write-Output "Navigation changes completed"
Apply master page
$MasterPageServerRelativeUrl = "/sites/rootsitename/_catalogs/masterpage/custommasterpage/SideBar.master"
Set-PnPMasterPage -MasterPageSiteRelativeUrl $MasterPageServerRelativeUrl -CustomMasterPageSiteRelativeUrl $MasterPageServerRelativeUrl -Web $web.ServerRelativeUrl
Write-Output "Masterpage set completed"
#Enable Access Request for the site to the Owners Group
$web = Get-PnPWeb
$web.SetUseAccessRequestDefaultAndUpdate($True)
$web.Update()
$web.context.load($web)
$web.Context.ExecuteQuery()
Write-Output "Access request settings completed"
#Apply logo from site coll library
Set-PnpWeb -SiteLogoUrl "/sites/rootsitename/Style%20Library/Images/GPLogo.png"
Write-Output "Logo apply completed"
Write-Output "All steps completed"
}
catch
{
Write-Output "Exception occured: start"
$_.Exception
Write-Output "Exception occured: End"
}
Create runbook in automation job:
Add PNP PowerShell module
Create runbook and choose latest runtime version, then copy all the PowerShell script from above and save, publish.
Call automation job from Microsoft Flow:
Add Create job action in flow and provide input parameters
Save details to SharePoint list from canvas PowerApps. Trigger flow on item create.
Start to final flow:
Comment for any clarifications or issues. Happy to help !!
Subscribe to my newsletter
Read articles from Jyothsna Radha directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Jyothsna Radha
Jyothsna Radha
I'm a passionate problem-solver who thrives on coding and tackling complex challenges. With deep expertise as an Okta administrator, I specialize in Single Sign-On (SSO) and Multi-Factor Authentication (MFA). My experience spans numerous integration and deployment projects on Azure. I've also led successful digital transformation initiatives using Microsoft's low-code/no-code solutions, particularly the Power Platform.