Here, I have a scenario where we are using and customizing SharePoint Pages in classic mode on SharePoint Online and want to view all the content in a well formatted email. I have the Publishing Feature enabled for my SharePoint Site and upload all my images in Images Library first and then refer them on the Page.
Please find below the steps to create the Flow,
- Start with When a file is created trigger
- Initialize a string variable to hold links to the Videos if any are embedded on the page
- Now let’s work on getting the SharePoint Page Body content from Pages Library by adding “Send an http request to SharePoint” action and configure it as follows. This is going to return all the Fields including the ones which store the HTML for the SharePoint Page consisting of Text, Images and Videos.
- The above will return a lot of fields, the ones we are going to run manipulations on are
- 'PublishingPageContent' which holds the HTML for the page
Getting Video Links and recreating HTML for our email
- We will focus on gathering the links to Videos if, any. Videos are embedded as iframe tags in the HTML Content, so let’s use split expression to divide the entire HTML string into an array divided by the keyword ‘iframe’
split(body('Send_an_HTTP_request_to_SharePoint')['d']['PublishingPageContent'], 'iframe')
- Above expression will return an array split at the points where the phrase ‘iframe’ was found, we want to capture only the items where the links to videos are found in this array. To do this, add a Filter Array action and configure it as shown below
- If the Filter Array expression indeed returns any output, we want to further create some HTML to nicely display them in an email, so we will add some HTML content in the variable initialized before
- Next, we want to extract the link part only from the HTML strings in the array returned in Step 5 and use them in recreated HTML for our email. With this extraction of Video links and recreating the HTML to display them in our email is complete.
Expression used in Compose in the diagram above –
replace(substring(split(item(), 'src="')[1], 0, indexOf(split(item(), 'src="')[1], '"')), ':', ':')
Expression used in Append to String variable in the diagram above -
<a href="@{outputs('Compose_4')}">Click here</a><br>
Getting Images, their width and recreating HTML for our email
- Next, we will focus on gathering the links to images and their widths to maintain the same size in the email, but first let’s initialize following variables whose uses are described in later steps
- An Array Variable
- A Final String variable of type String, set the value as body('Send_an_HTTP_request_to_SharePoint_2')['d']['PublishingPageContent']
- A Counter Variable of type Integer with value 0
- A Width Variable of type String
- Add a Compose action and Split the PublishingPageConent from step 3 by ‘’PublishingImages/” to get an array of strings
split(body('Send_an_HTTP_request_to_SharePoint_2')['d']['PublishingPageContent'], 'PublishingImages/')
- Add a Filter Array action and pass the output from the previous step and add the below condition. This is to capture only the strings which consist of images
- Add an Apply to Each action, pass the Array output from previous step, inside this action add the below ones
- We need to check if the images have any applied width on the page, to do this, we need to check if the current string item consists of the phrase “width”
contains(items('Apply_to_each'), 'width')
- If this is true, we want to extract the number assigned to width attribute and run below manipulation operations and replace the image tags with our manipulated string in the Final String variable
- Add a compose with this expression
split(split(items('Apply_to_each'), 'width:')[1], 'px')[0]
- Recreate the HTML with the number fetched from above, set the Width variable to below HTML string
width=@{outputs('Compose_9')} alt
- Replace the current item’s “alt” with “Width” variable value and store it in Compose
replace(items('Apply_to_each'), 'alt', variables('Width'))
- Replace the current item available in the Final String variable with above output and store it in Compose
replace(variables('String'), items('Apply_to_each'), outputs('Compose_10'))
- Finally Set the Final String Variable with Output from above step
- Increment the Counter variable by 1
- From here on, I am going to number the Compose actions to understand where their outputs are being used.
- Add a Compose 1 and split it by ‘”’ (double quote) to extract the name of the image
split(items('Apply_to_each'), '"')[0]
- Add Compose 2 to concat your relative SharePoint Library URL (hardcoded) with the Image Compose 1 output
- Add Compose 3 and configure it as follows
- Add Compose 4 and use it to replace instances of Compose 2 found in Final String variable with Compose 3
- Add Get file content action and pass the below URL
- /Library Name/Compose 1 Output
- Add Append to Array Variable, pass Array Variable here and configure it as below
{
“Name”:”Compose 3 Output”,
“ContentBytes”:Body from above step
}
- Finally Set the Final String variable with Compose 4 output
Convert relative URLs to absolute for them to work in Outlook
- Step out of the Apply to each now, and add a Compose action to replace breaks with emtry string and relative URLs with absolute ones
- replace(replace(variables('String'),'<br>',''), '/sites', 'https://yourdomain.sharepoint.com/sites')
Send Email
- Create the HTML Format of your choice here I am just adding a table and injecting my HTML content the way I want them to be displayed in the email
- Click on the advanced options, and in the Attachments field switch to text mode and add the Array variable