Automate SharePoint Sub Site Creation with Power Automate Flow

Introduction

In this article, we will see how to automate the subsite creation process for the SharePoint site using a SharePoint list and Power Automate flow. We want to give subsite creation capability to a few business users who are not site owners. Still, they have access to a SharePoint list where they can add a new entry by specifying the Title and Description of the subsite the subsite is automatically created for them with the help of Power Automate flow, and the subsite URL is also updated in the SharePoint list. Site owners and other business users can use this list to track the details of all subsites, like the total number of subsites, when and who create them, etc.

Create a SharePoint list for storing subsite information

First, we will create a SharePoint list, 'subsite Requests' and remove all its permissions for all SharePoint groups except a group where we have all the business users who need the capability of subsite creation.

Automate SharePoint subsite Creation with Power Automate Flow

This list has 3 fields: Site Title for the Title of the subsite. Description for a description of the subsite and SiteUrl to show the link/ URL of the subsite.

Power Automate flow for automation of site creation

Now we will create a Power Automate flow with an account that has site collection administrator access for the parent site. This will be triggered by When an item is created event of the subsite Requests list.

Automate SharePoint subsite Creation with Power Automate Flow

Flow variables to store information

In flow, define the following variables to store information like subsite Title from the list, Site Url, and Full Subsite Url to store information for the final link of the subsite created by this flow.

Automate SharePoint subsite Creation with Power Automate Flow

We will create a subsite URL by removing spaces and special characters from the subsite title provided by the user in the SharePoint list. For this will set varSiteUrl to varSiteTitle.

Automate SharePoint subsite Creation with Power Automate Flow

Try Catch blocks

Add Try Catch blocks for exception handling. We will add all our flow logic inside the Try block. If you want to learn more about exception handling in Power Automate, you can refer to my article on C# Corner at this link: https://www.c-sharpcorner.com/article/exception-handling-in-power-automate-flow/

Automate SharePoint subsite Creation with Power Automate Flow

Create a special character array

Use Compose action to create a special character array. Use the following expression:

createArray('',':', '#',' %','" ','" ','*', '$',' @')

Automate SharePoint subsite Creation with Power Automate Flow

We will use this array to check and remove any special character from the subsite Title.

Replace special characters from the site URL

Automate SharePoint subsite Creation with Power Automate Flow

Iterating output from Compose - Create special character array action. We will use Compose action to create another array without any special character. The following expression is used in Compose - Replace Special Characters action.

replace(variables('varSiteUrl'),item(),'')

Finally, the output of this Compose action is set as varSiteUrl.

Create subsite

Now we will use Send an HTTP request to SharePoint action to Create a subsite.

Automate SharePoint subsite Creation with Power Automate Flow

The following are the parameters:

Site Address: Link of your SharePoint site where you want to create subsites.

Method: POST

Uri: /_api/web/webinfos/add

Headers:

{
  "Accept": "application/json;odata=verbose",
  "Content-Type": "application/json;odata=verbose"
}

Body:

{'parameters':
{ '__metadata':
{'type': 'SP.WebInfoCreationInformation'},
'Url':'@{variables('varSiteUrl')}',
'Title':'@{variables('varSiteTitle')}',
'Description':'@{triggerOutputs()?['body/Description']}',
'Language':'1033',
'WebTemplate':'STS#3',
'UseUniquePermissions':true
}
}

Get the URL of the newly created subsite.

Use Parse JSON action to use the response of the "Send an HTTP request to SharePoint" action to get the Site URL of the newly created subsite.

Automate SharePoint subsite Creation with Power Automate Flow

Content: Body from action Send an HTTP request to SharePoint - Create a subsite

Schema to parse JSON:

{
    "type": "object",
    "properties": {
        "d": {
            "type": "object",
            "properties": {
                "__metadata": {
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "string"
                        },
                        "uri": {
                            "type": "string"
                        },
                        "type": {
                            "type": "string"
                        }
                    }
                },
                "Configuration": {
                    "type": "integer"
                },
                "Created": {
                    "type": "string"
                },
                "Description": {
                    "type": "string"
                },
                "Id": {
                    "type": "string"
                },
                "Language": {
                    "type": "integer"
                },
                "LastItemModifiedDate": {
                    "type": "string"
                },
                "LastItemUserModifiedDate": {
                    "type": "string"
                },
                "ServerRelativeUrl": {
                    "type": "string"
                },
                "Title": {
                    "type": "string"
                },
                "WebTemplate": {
                    "type": "string"
                },
                "WebTemplateId": {
                    "type": "integer"
                }
            }
        }
    }
}

Set variable varSiteUrl to ServerRelativeUrl we get from Parse JSON action.

Automate SharePoint subsite Creation with Power Automate Flow

Set variable varFullSubsiteUrl to your SharePoint root site plus varSiteUrl

Automate SharePoint subsite Creation with Power Automate Flow

Update subsite URL in subsite Requests list

Now, we have created the subsite and got its full URL. We must update the site creation list of subsite Requests with the link/ URL of this subsite.

To update the subsite Requests list with the link of the subsite, we will use the action Send an HTTP request to SharePoint to update the SiteUrl field in the list.

Set the Site URL value to a Hyperlink field in the SharePoint List

Automate SharePoint subsite Creation with Power Automate Flow

The following are the parameters:

Site Address: Link of your parent SharePoint site.

Method: POST

Uri: _api/web/lists/GetByTitle('<SubSiteCreationListInternalName>')/items(@{triggerOutputs()?['body/ID']})

Headers:

{
  "Accept": "application/json;odata=verbose",
  "Content-Type": "application/json;odata=verbose",
  "IF-MATCH": "*",
  "X-HTTP-METHOD": "MERGE"
}

Body:

{
  "__metadata":{
    "type": "SP.Data.<SubSiteCreationListInternalName>ListItem"
  },
  "SiteURL" :{
    "__metadata":{type:"SP.FieldUrlValue"},
    "Description":"@{variables('varSiteTitle')}",
    "Url":"https://xyz.sharepoint.com@{variables('varSiteUrl')}"
  }
}

Here SubSiteCreationListInternalName means the internal name of SharePoint list subsite Requests.

Summary

In this article, I explained the steps to automate the SharePoint subsite creation process with the help of the SharePoint list and Power Automate flow. People with edit access in the SharePoint list can add details like the Title and Description of the subsite and save it. Once the item is created in the list, a flow will trigger and create a subsite in the site with the URL the same as the Title but without spaces and special characters. Finally, the flow will update the subsite Requests list with the link/URL of the subsite. This functionality will help the site owners see how many subsites are created with their details, like who requested those sites and when. They need to add Created and Created By fields of the subsite Requests list in the default view.


Similar Articles