Site Scripts with Power Automate - leberns/SharePoint GitHub Wiki
We are using Power Automate to create a site collection and site scripts to provision artifacts, like lists, on that site.
The solution uses two flows, one flow to create the site and another for the artifacts.
The users create project sites by adding items into a Projects list.
The project site has a Mile Stones list.
The title and URL of the site are set as per the title of the project list item.
The Projects list with one project:
The created project site:
This flow is triggered when an item is created on a SharePoint list, in our example, the Projects list.
The URL of the site is definied in a Compose action where the title of the item has its spaces removed and it is encoded.
This is the function used in the Compose action, note it needs improvement to support the characters for SharePoint URLs:
uriComponentToString(replace(triggerBody()?['Title'], ' ', ''))
Input example: https://my-tenant.sharepoint.com/sites/Project $ 123^
Encoded output example: https://my-tenant.sharepoint.com/sites/Project$123%5E
The action "Send an HTTP Request to SharePoint" is used to create the site.
Once the HTTP request is done the flow updates the item with the HTTP response. The link to the site is set with another POST, for details see Update a Hyperlink Column in SharePoint with Power Automate.
Action: Send an HTTP Request to SharePoint
POST
_api/SPSiteManager/create
headers:
accept:application/json;odata.metadata=none
content-type:application/json;odata=verbose
body:
{
"request": {
"Title": "MySite 01",
"Url":"https://contoso.sharepoint.com/sites/MySite01",
"Lcid": 1031,
"ShareByEmailEnabled":false,
"Classification":"Low Business Impact",
"Description":"Description of MySite 01",
"WebTemplate":"SITEPAGEPUBLISHING#0",
"Owner":"[email protected]"
}
}
HTTP response example:
{
"d": {
"Create": {
"__metadata": {
"type": "Microsoft.SharePoint.Portal.SPSiteCreationResponse"
},
"SiteId": "077c2890-e389-4a18-ab27-5a2ba0732fa5",
"SiteStatus": 2,
"SiteUrl": "https://my-tenant.sharepoint.com/sites/ProjectGamma"
}
}
}
More about how to create a site using the SharePoint REST API: Manage modern SharePoint sites using REST
This flow is triggered when the project item is changed, as it is by the create site flow.
An advantage of calling the site script when the item changes is that it is possible to execute it several times, if desired to apply an updated site script to an existing site.
The URL of the site is parsed from the hyperlink URL field using the function:
split(triggerOutputs()?['body/ProjectUrl'], ',')[0]
Example of a URL value: https://my-tenant.sharepoint.com/sites/ProjectGamma, Project Gamma
Parsed URL: https://my-tenant.sharepoint.com/sites/ProjectGamma
The site script is executed using the SharePoint REST API.
The Compose action is used to define the site script by concatenating JSON strings and replacing parameters.
For example, to set the title of the site according to the title of the list item, see setDescription
below.
{
"verb": "createSPList",
"listName": "MileStones",
"templateType": 100,
"subactions": [
{
"verb": "setTitle",
"title": "Mile Stones"
},
{
"verb": "setDescription",
"description": "Mile stones for @{triggerOutputs()?['body/Title']}"
},
{
"verb": "addSPField",
"fieldType": "Text",
"displayName": "Description",
"isRequired": false,
"addToDefaultView": true
},
{
"verb": "addSPField",
"fieldType": "Number",
"displayName": "Mile Stone Weight",
"internalName": "MileStoneWeight",
"addToDefaultView": true,
"isRequired": true
}
]
}
Action: Send an HTTP Request to SharePoint
POST
_api/Microsoft.Sharepoint.Utilities.WebTemplateExtensions.SiteScriptUtility.ExecuteSiteScriptAction()
headers:
accept:application/json;odata.metadata=none
content-type:application/json;odata=verbose
body:
{
"actionDefinition": "@{string(outputs('SiteScript_Compose'))}"
}
Flow for ExecuteSiteScriptAction
Site design / Site script JSON schema
Functions in expressions for Azure Logic Apps and Power Automate
How to provision a SharePoint list with ExecuteSiteScriptAction in Power Automate
Or do other operations related to site scripts:
Site design: PnP PowerShell cmdlets
Site design: PowerShell cmdlets
Building a Microsoft Teams Provisioning Process using Power Apps & Power Automate
Calling the PnP provisioning engine from a site script
SharePoint PnP Provisioning with Azure Logic Apps and Azure Automation