Introduction
Creating and managing SharePoint sites quickly and easily is important for any organization. By using PowerShell and Power Automate, you can automate site creation and include useful workflows in your site templates. This guide will show you how to create a custom SharePoint site template that makes setting up new sites simple and consistent.
Key Features of This Approach
- Power Automate Integration: Automatically run workflows whenever a new site is created.
- Custom Site Scripts: Use simple scripts to define how your sites look and work.
- Easy Site Management: Set up themes, time zones, and site logos with just a script.
- Workflow Automation: Automatically send notifications or track site creation details using workflows.
Benefits
- Saves Times: Automates repetitive work, freeing up time for your team.
- Keeps Things Consistent: Makes sure every new site follows the same setup and rules.
- Scalable: Handles creating lots of sites without manual work.
- Easy to Customize: You can change templates anytime to match your needs.
Steps to Implement
1. Set up a Power Automate Flow
Power Automate will handle workflows triggered during the site creation process.
Steps to create the flow
- Go to Power Automate.
- Click “New Flow” > “Automated Cloud Flow”.
- Name the flow (e.g., “PowerShell through calling a Power Automate”) and click “Skip”.
- Add the “When an HTTP request is received” trigger.
- Configure the trigger settings.
- Under “Who can trigger the flow?” select “Anyone”.
- Add the following Request Body JSON Schema.
{
"type": "object",
"properties": {
"type": {
"type": "string"
},
"properties": {
"type": "object",
"properties": {
"webUrl": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"parameters": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"properties": {
"type": "object",
"properties": {
"siteName": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"product": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
}
}
}
}
},
"webDescription": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"creatorName": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"creatorEmail": {
"type": "object",
"properties": {
"type": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"createdTimeUTC": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
}
}
}
}
}
- Add any additional actions, such as sending an email creating a list or library, or creating a log.
- Save the flow to automatically generate the HTTP POST URL. Copy this URL for use later.
2. Write a Site Script
A site script defines the actions SharePoint will execute when the template is applied.
Steps to create the script
- Open a text editor. (e.g. Notepad++ or Visual Studio Code).
- Copy and customize the following JSON structure.
$JSONScript = @"
{
"$schema": "schema.json",
"actions": [
{
"verb": "applyTheme",
"themeName": "Custom Theme"
},
{
"verb": "setRegionalSettings",
"timeZone": 24,
"sortOrder": 25,
"hourFormat": "12"
},
{
"verb": "setSiteLogo",
"url": "/Style Library/logo.jpg"
},
{
"verb": "createSPList",
"listName": "Work",
"templateType": 101,
"subactions": [
{
"verb": "setTitle",
"title": "Work"
}
]
},
{
"verb": "triggerFlow",
"url": "PASTE_YOUR_FLOW_URL_HERE",
"name": "PowerShell through calling Flow",
"parameters": {
"siteName": "Demo site",
"product": "SharePoint Online"
}
}
],
"bindata": {},
"version": 1
}
"@
- Replace PASTE_YOUR_FLOW_URL_HERE with the URL copied from your Power Automate flow.
3. Create a site Template using PowerShell
PowerShell allows you to upload and apply the site script to create a template.
Steps to run the script
- Open PowerShell as an administrator.
- Connect to SharePoint Online.
Connect-SPOService –Url https://your-tenant-name-admin.sharepoint.com
- Add the site script.
$JSONScript = Get-Content -Raw -Path “PathTo\SiteScript.json”
$SiteScript = $JSONScript | Add-SPOSiteScript -Title “Demo Site Script”
- Create the site design.
Add-SPOSiteDesign -Title “Site Template1” -WebTemplate 68 -SiteScripts $SiteScript.ID -Description “Site Template1”
- WebTempalte: User 68 for communication sites or 64 for Team Sites or 1 for Team sites without Group.
- Disconnect from SharePoint Online.
Disconnect-SPOService
4. Create a New Site
Use the SharePoint Admin Center to create a new site with the custom template.
Steps to create the site
- Navigate to https://your-tenant-name-admin.sharepoint.com.
- Select Active Sites > Create.
- Choose the site type. (e.g. Communication Site).
- Under Template, select Your Organization > Your custom template and Click on "Use Template".
- Enter the required site details.
- Site Name: Provide a site name (e.g. “Demo Communication Site”).
- Site Description: Add a brief description.
- Site Owner: Specify the owner.
- Click Create Site and wait for provisioning to complete.
5. Test the Automation
- Open the newly created site.
- Verify the following.
- The applied theme and regional settings.
- The uploaded site logo.
- The created SharePoint list.
- The triggered Power Automate workflow.
6. Manage Templates
To remove an existing template
- Reconnect to SharePoint Online in Power Shell.
Connect-SPOService -Url https://your-tenant-name-admin.sharepoint.com
- Get the list of site designs.
Get-SPOSiteDesign
- Delete the template by ID.
Remove-SPOSiteDesign -Identity “TemplateID”
Additional Tips for Beginners
- Test Before Deployment: Always test in a development environment.
- Error Handling: Enable logging in Power Automate for debugging issues.
- Template Naming: Use meaningful full names for scripts and templates to avoid confusion.
- Keep Scripts Modular: Break down complex actions into smaller scripts for easier management.
Advantages
- Automates complex workflows during site creation.
- Centralizes configurations, making maintenance easier.
- Enhances productivity by reducing administrative overhead.
Pros and Cons
Pros |
Cons |
Consistent site creation process. |
Initial setup requires scripting expertise. |
Fully customizable based on organization needs. |
Debugging errors may require deeper knowledge. |
Supports integration with other services. |
Depends on Power Automate flow reliability. |
Additional Tips
- Test the JSON schema and PowerShell scripts in a development environment before deploying.
- Use descriptive names for site templates to avoid confusion.
- Regularly review and update site scripts to align with organizational changes.
Conclusion
Automating SharePoint site creation using PowerShell and Power Automate transforms how you manage site provisioning. This approach enhances efficiency, consistency, and scalability, allowing IT administrators to focus on strategic initiatives. By following this guide, you can create powerful site templates integrated with workflows that meet your organization's needs.