Create a SharePoint (Online) Subsite from Power Automate (Flow)

5 min read

Recently we came across a requirement for SharePoint Online where we were required to create SharePoint subsite when an item is created in the SharePoint List. It was basically a Projects list and when a new project is created in the list, the requirement was to create a new Project Subsite when a new Project item is created on the list.

So, we will do the following in the Flow:

  1. Create a new subsite when the new SharePoint List Item is created.
  2. Update the SharePoint List Item by setting the Site URL of the newly created subsite in the list item column ‘Site URL.

Create a new Flow:

  1. To create the Flow, Go to Power Automate -> Create -> Start from Blank -> Automated Flow
  2. Give an appropriate name for the Flow and select the “When an item is created” trigger and click on Create button

New call-to-action

3.  Select the SharePoint Site and the list for which you want to create the Flow.

We will be using Title column value from the list as Site Title, and we will remove the special characters and spaces from Title column value to use it as Site URL

4. Initialize Variables for Site Title and Site URL

5. To replace special characters and spaces from Title value to get the Site URL, create an Array using the Compose action 

Use following expression:

createArray(‘ ‘,’:’,’#’,’%’,'”‘,'”‘,’*’,’?’,’/’,’\’,’|’,'<‘,’>’,'{‘,’}’,'(‘,’)’,’$’,’@’) 

a. In an Apply to Each loop, replace special characters from Subject. Use the output from the previous step, i.e. the previous compose Action output: 

b. Compose – Replace Special Characters 

Use the expression: replace(variables(‘SiteURL‘),item(),”)

c. Set Variable – Save the output of Replace special characters to variable

6. Now create the SharePoint site using Rest API, use the “Send an HTTP request to SharePoint” action make the call the Rest API. 

Following values to be configured in the “Send an HTTP request to SharePoint” action to create a new site

a. Site Address: Provide the Site Collection URL where you want your new sites to be created. Make sure you have permission to create a new site in that Site Collection or using the appropriate account in the Connection, which has required permissions. 

b. Method: Select “POST” 

c. Uri: /_api/web/webinfos/add 

d. Headers: Use the following header key-value pairs 

Accept 

application/json;odata=verbose 

Content-Type 

application/json;odata=verbose 

 

e. Body: Copy-paste following JSON to the Body input box 

 

{ 'parameters': 

{ '__metadata': 

{ 'type': 'SP.WebInfoCreationInformation' }, 

'Url':'@{variables('SiteURL')}', 

'Title':'@{variables('Site Title')}', 

'Description':'Project Site', 

'Language':'1033', 

'WebTemplate':'STS#3', 

'UseUniquePermissions':false 

} 

} 

i. You may want to change the following values in the JSON 

  1. URL: Use the variable which is initially defined in the Flow for Site URL.
  2. Title: Use the variable which is initially defined in the Flow for Site Title. 
  3. Description: This could be any appropriate text 
  4. Language: The language you want your new site to be created in 
  5. WebTemplate: STS#3 value which I have provided is for Modern Team Site template, you could use any other value, you can look up for Template ID of the site template you may want to use via a quick search. 
  6. UseUniquePermissions: I have provided value ‘false’, as I want the new site to inherit permissions of Parent Site. If you need a new site to have unique permissions, then provide the value ‘true’ (without the quotes). 

7.  Now you have done all the actions to create a new site, once the site is created, we also want to set the newly created Site URL to be updated in the List Item. 

8. You can use the response of “Send an HTTP request to SharePoint” action, to get the Site URL of the newly created Site.  

</p> </br> 

a.  In order to do that first we will parse the JSON response using the Parse JSON action, so add Parse JSON action to the Flow 

b. Now click on “Generate from sample” button available under the Parse Json Action 

c. Copypaste below JSON in the sample JSON payload box and click on the “Done” button 

 

{ 

  "d": { 

    "__metadata": { 

      "id": "https://<siteurl>/_api/web/webinfos/add", 

      "uri": "https://<siteurl>/_api/web/webinfos/add", 

      "type": "SP.WebInformation" 

    }, 

    "Configuration": 0, 

    "Created": "2019-11-25T16:48:15", 

    "Description": "Project Site", 

    "Id": "09e4283a-d86d-4aad-8ba7-eb1b9c9914ea", 

    "Language": 1033, 

    "LastItemModifiedDate": "2019-11-25T16:48:26Z", 

    "LastItemUserModifiedDate": "2019-11-25T16:48:26Z", 

    "ServerRelativeUrl": "/sites/test1", 

    "Title": "Test1", 

    "WebTemplate": "XXX", 

    "WebTemplateId": 0 

  } 

} 

d. Now get the value from the Site URL from parsed JSON, so use Set Variable action, as shown below 

e. Now we will set the Site URL value to a Hyperlink field in the SharePoint List.  Again, we will do this using Rest API, use the “Send an HTTP request to SharePoint” action make the call the Rest API: 

Following values to be configured in the “Send an HTTP request to SharePoint” action to set hyperlink field value:

a. Site Address: Provide the Site Collection URL where you want your new sites to be created. Make sure you have permission to update items in the SharePoint List or use the appropriate account in the Connection, which has required permissions. 

b. Method: Select “POST” 

c. Uri: _api/web/lists/GetByTitle(‘Projects’)/items(@{triggerBody()?[‘ID’]}) 

Note: We will need to provide the item id of the item which needs to be updated, that can be provided from the trigger dynamic content as the trigger is on a new item created. 

d. Headers: Use the following header keyvalue pairs 

Accept 

application/json;odata=verbose 

Content-Type 

application/json;odata=verbose 

IF-MATCH 

* 

X-HTTP-METHOD 

MERGE 

 

e. Body:  Copy-paste following JSON to the Body input box 

{ 

  "__metadata":{ 

    "type":"SP.Data.Projects_x0020_ListListItem" 

  }, 

  "project_x0020_link":{ 

    "__metadata":{type:"SP.FieldUrlValue"}, 

    "Description":"@{variables('Site Title')}", 

    "Url":"https://<siteurl>@{variables('SiteURL')}" 

  } 

} 

i. You may want to change the following values in the JSON: 

  1. data-contrast="auto">Type: Needs to be in the following format, where replace <ListName> with your List URL value. So, e. g. the list I used has URL as /Lists/Projects%20List/AllItems.aspx, the <ListName> will be replaced by Projects_x0020_List, so we _x0020_ needs to replace the %20 in the URL. Note here it’s not the List Title but the List URL value. 

Format: SP.Data.<ListName>ListItem 

   2.  My field to be updated is “project_x0020_link”, you have to use field internal name here.  

   3.  As my field is of type hyperlink the type value is SP.FieldUrlValue 

   4. Hyperlink field consists of two parts one URL, and the other is Description, so where we are setting Description with Site Title variable, and URL with the Site URL variable

a. Please note to replace <siteurl> with your Site Collection URL. 

 

References: 

SharePoint Rest API Samples 

Power Automate Learning Path 

Recent Posts