Automate Modern SharePoint Page Creation Using Power Automate

Overview

 
This blog will help you create an Automation of Modern Page Creation in SharePoint Using Power Automate.
 
We will use email receiving as the trigger. But we can use SharePoint List as well.
 
Step 1 - Create a Template 
 
A page template needs to be created as this will be used as a template for all the new pages that we will create using the automation.
 
For the demo we will create a simple page with text webpart where we will add all the required content and name the template as blanktemplate
 
Step 2 - Create a Power Automate Flow
  1. We will create an automated flow triggered when an email is received from a particular email id
  2. [Action :- send an HTTP request to SharePoint] Now the first step is to create the page,
We will use copy to end point to create the page from the template
 
Uri :- _api/web/getfilesbyserverrelativeurl('/sitepages/templates/blanktemplate.aspx')/copyto
(strnewurl='/sitepages/@{triggerBody()?['subject']}.aspx',bOverwrite=true)
  1. [Action :- Get file metadata] We will require the Id of the page which was created for checking out the page and
updating the content of the page so the next step is to add get file metadata
 
File Identifier :- SitePages%2f@{triggerBody()?['subject']}.aspx
 
Note
We will have to replace "/" with its uri encoded value "%2f"
  1. [Action :- send an HTTP request to SharePoint] We will need to check out the page so that we can add content to the page
Uri :- _api/sitepages/pages(@{body('Get_file_metadata')?['ItemId']})/checkoutpage
  1. [Action :- Compose] To use the HTML data which is coming from the Mail we would require to change the data.
      • Replace double quotes(") in HTML to (\\\") so that it can be used in the JSON object while saving the data
        1. replace(uriComponent(triggerBody()?['body']),'%22','%5C%5C%5C%22')
      • Replace white space in HTML with blank text
        1. uriComponentToString(replace(outputs('Compose'),'%0D%0A',''))
  1. [Action :- send an HTTP request to SharePoint] we will now need to save the page as a draft and send the JSON object in the body.
Uri :- _api/sitepages/pages(@{body('Get_file_metadata')?['ItemId']})/savepageasdraft
  1. [Action :- send an HTTP request to SharePoint] we will now publish the page using the below endpoint.
Uri :- _api/sitepages/pages(@{body('Get_file_metadata')?['ItemId']})/Publish
 
 
Step 3
 
How to get JSON for saving the page
  1. Edit any existing page created from template
  2. Click on publish (open the developer tool by pressing F12)
  3. Got to the network tab in the developer tool and search for SavePage
Click on the request at the bottom. We will  get request payload which will be used for the request body.
 
 
Outcome
 
Received Email 
 
 
Created Page