SharePoint Online Site Provisioning using Logic App and Custom List


In this blog “SharePoint Online Site Provisioning using Logic App and Custom List”, you will learn how to develop a fully automated solution to create a site collection in SPO (SharePoint O365) for submitted new site creation request(s) in a custom list. I have used a SharePoint Custom list & Azure Logic App to develop this solution. Developers who are not aware of the Logic App should read about it here, it is very similar to Power Automate. I read several blogs on Power Automate, Azure Function App & Azure Logic App to design a logic that can automate SharePoint site collection provisioning.

Logic App is a cloud-based solution to automate and simplify a business process more effectively. This can be achieved by developing an automated workflow using Logic Apps.

Topics to be covered in this blog

  • 1st Step – Create a custom list for new site requests.
  • 2nd Step – First Logic App for Approval workflow on each item.
  • 3rd Step – Second Logic App to create a site post-approval.
    • Update the status in the site creation request list once the site is created.
  • Step 4 – Error handling (send an email on an error).

Step 1 – Create a custom list for new site requests

Create a custom list named – Site Creation Request. We will create a SharePoint list with the following columns (Figure – 1).
RootSiteURL, Owner, Site Template, Requestor, Approved, Site Created, Comments, Status, SiteCollectionAdmin

Note: Changed the display name of the Title column to RootSiteURL.

Fig 1. Columns in Site Creation Request

Site Template Column contains the Site Template ID in the given format:

  • Teams – STS#3
  • Communication Site – SITEPAGEPUBLISHING#0
Fig 2. Item added to Site Creation Request list

Step 2 – First Logic App for Approval workflow on each item

  1. Create a Logic App named Logic-App-Approval for Approval process. Fill in all the required details and click on Review + Create button
Figure 3 – Create an Approval Logic App

2. Once the Logic app is created, choose a trigger to run Logic App, in this case, app will run on “When an item is created”. Here in Logic App, we must define the schedule, how often we want to check the request list for new items (only difference in Power Automate item creation trigger and Logic App Item creation trigger).

Figure – 4: When an Item is created trigger

3. Add 2 Initialize Variable actions for ErrorInformation and ErrorInnerException

Figure – 5 : Initialize Variable Actions

4. Add a Scope control and rename it to Business Scope, this action will work as a container for all the other actions.

Figure – 6 : Choose Scope Control

5. Start adding all the actions inside Business Scope now
6. Add and configure “Send an email action” to send an acknowledge to requester.

Figure – 7 : Add Send an email (V2) action

7. Add and configure “Update Item” to Set the status of current item to In-Progress

Figure – 8 : Add Update Item action
Figure – 9: Configure Update Item action

8. Add and configure “Approval Email” action to send an approval email to Approver with option Approve and Reject

Figure – 10 : Add Send approval email action
Figure – 11 : Configure Send approval email action

9. In Approval Email action in Logic App, we don’t have an option to provide comment in the email itself on Approval/Rejection (By default this functionality is available in Power Automate Approval email action). To overcome this situation, I have created “Comment” Column in the list and providing an item link in approval email so that Approver can give the comment directly in the list item using the provided link, before clicking on Approve and Reject from the email.

10. After that, add “Get Item” SharePoint action to get the updated list item (If in-case Approver has provided any comment)

Figure – 12 : Configure Get item action

11. Add “Condition Control” and put User response to check if new site creation request has been approved or rejected.

Figure – 13 : Add Condition Control
Figure – 14 : Configure Condition Control

12. If the request has been approved, In If true Section on the left-hand side:

  • Add a “Scope control” as shown in step 4 and rename it to “Main Scope”, I have added this scope for better error handling and performing site provisioning process inside this block.
  • In the Main Scope, add an “Update item” action to change the status to “Approved” (as shown in step 7.
  • Now, add the “Compose” action to get the ID of the current item.
Figure – 15 : Configure Compose Action

  Note: In my scenario, I have created another logic app to create a Site, but it can be done in the same approval logic App (Totally depend on the requirement).

  • To call a second logic app from this app, First, Create Logic App to provision site and then Add “Logic App” action to “Logic-App-Approval” (first logic app) to call “Logic-App-CreateSite” (second logic App).
Figure – 16 : Add Azure Logic Apps action
Figure – 17 : Configure Azure Logic Apps to call Logic-App-Create

Note: Steps to create “Logic-App-CreateSite” in the document below.

  • Finally, “If true” block should be shown as below
Figure – 18 : Final If true block

13. If the request has been rejected, In “If false” Section on the right-hand side:

  • Add Send an email (V2) action to send rejection email to requester.
  • Add Terminate action to just complete the process.
  • Finally, “If false” block will look like as shown below
Figure – 19 : Final If false block

14. Finally, complete “Business scope” should be shown as below:

Figure – 20 : Complete Business Scope Block

15. Complete “Logic App Approval” should be shown as below:

Figure – 21 : Complete Logic-App-Approval app

Step 3 – Create a Second Logic App to create a site post-approval

1. Create new “Logic App” named “Logic-App-CreateSite” for site provisioning (Follow step 1 of the First Logic App)
2. Add a trigger “when new http request is received” as this Logic App will be called from the Approval Logic App. In “Request Body JSON Schema” of a trigger action, add below JSON value because this Logic App will accept “ID” as a parameter from the Caller Logic App. (Note: Any number of parameters can be passed).

{
    "properties": {
        "ID": {
            "type": "string"
        }
    },
    "type": "object"
}
Figure – 22 : Configure When a HTTP request is received trigger

Note: – To generate an HTTP POST URL, add any action below the trigger action, and Save the logic app. This will generate the HTTP URL which will be used by the Caller app to make an HTTP call.

3. Now, add 3 “Initialize variable” action (as shown in Step 3 of First Logic App) for ListName, TenantUrl, vIsUniquePermission (Optional) of String type, and set the required values as per your tenant.

Figure – 23 : Configure Initialize Variables

4. Add “Compose” action to get the item ID from the HTTP Post request.

Figure – 24 : Configure Compose Action to set get the Item ID

5. Add “Get Item” action and pass Output of the compose action in Id field to fetch the item from the Site Creation List.

Figure – 25 : Configure Get item action

6. Add Compose action to get the value of “Site Template ID” column of the current item. As in the list, I am allowing user to select template type from the dropdown in this format (TeamSite – STS#3). So, we must get the template id (STS#3) from the selected value, hence splitting the Selected Site Templated Id by ‘-‘ and getting only SharePoint Template ID from the value, by using the below formula

split(body('Get_item')?['SiteTemplate']?['Value'],'-')
Figure – 26 : Configure compose action to get template id

7. Now, add “Send an HTTP request to SharePoint” action to construct and execute SharePoint REST API call to create a site based on the parameters. Here are the details of the mapping request:

Site Address – Mapped to the Title (RootSiteURL) column.
Method – POST
URI - /_api/SPSiteManager/create
 
Header -
{
"Accept": "application/json;odata.metadata=none",
"odata-version":"4.0"
}
Body: 
{
  "request": {
    "Title": "@{body('Get_item')?['Site']}",
    "Url":"@{concat(body('Get_item')['Title'],'/sites/',body('Get_item')?['Site'])}",
    "Lcid": 1033,
    "ShareByEmailEnabled":false,
    "Description":"Flow testing",
    "WebTemplate":"@{trim(outputs('Get_Site_Template_ID')[1])}",
    "Owner":"@{body('Get_item')?['SiteCollectionAdmin']?['Claims']}"
 }
}
Body Parameter details: 
Title: Mapped to the column Site.
URL: Concatenating the Title field and Site field to make the correct URL.
WebTemplate: Get the output from the Site Template ID action.
trim(outputs('Get_Site_Template_ID')[1])
Owner: This is a primary Admin of the site collection. I am mapping with the SiteCollectionAdmin field of an item.
Figure – 27 : Configure HTTP request to create site collection in SPO

8. Now, add “Send an HTTP request to SharePoint” to Update “Site Created” column of the current item to ‘YES’.  Below is the mapping request:

Site Address – Mapped to the Title field of the list, here I am mapping with the tenant URL variable, just to show, how can we use variable in Logic App.
Method – POST
URI - _api/web/lists/GetByTitle('@{variables('ListName')}')/items(@{triggerBody()?['ID']})
 
Syntax - _api/web/lists/GetByTitle('ListName')/items('ID')
 
Header -
{
"Accept":  "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"If-Match":"*",
"X-HTTP-Method": "MERGE"
}
Body –  
{
  '__metadata': {
    'type': 'SP.Data.Site_x0020_Creation_x0020_RequestListItem'
  },
  'SiteCreated': true
}

Note: Highlighted value is Static name of the Site Creation request List.

Figure – 28 : Configure HTTP request to Update list item

9. At the end, add “HTTP response” action to send a response back to “Approval Logic App” on successful completion.

Figure – 29 : Configure HTTP Response action

10. Completed Logic-App-CreateSite Logic App should look like as shown below

Figure – 30 : Completed Logic-App-CreateSite

Conclusion

This blog post SharePoint Online Site Provisioning using Logic App and Custom List describes how Logic Apps and REST API works together to achieve the site provisioning process.

Now if Incase, the logic app encountered an error during the provisioning process, how will we know where has an error occurred and what was the reason. So, to achieve that, you have seen above, I have added 2 scope controls and put all the actions inside that. Will see the actual “Error handling (send an email on an error)” in our next blog. Stay Tuned!

For any questions, please comment below or contact us

You May also like this :

How to customize mail notification in Azure Data Factory

Sharing is Caring

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top