Extracting SharePoint Online page content and embedding the HTML in an email
If you ever need to extract the HTML content from a modern SharePoint Online page, there is a REST API call you can use. I decided to use this HTML content and automatically insert this into an email. Therefore, when a user writes a news post on a SharePoint site, an email can also be sent using Power Automate to gain maximum impact and exposure.
Firstly, you need to create your SharePoint page in the Site Pages library on your site. Once you have created your page, use the following REST API call.
_api/web/lists/getbytitle('Site%20Pages')/items?$select=Title,CanvasContent1,FileLeafRef&$filter=FileLeafRef eq 'Page-Title.aspx'
From this call, you will receive the filename (FileLeafRef), page title (Title) and the file content (CanvasContent1). The CanvasContent1 is the HTML of the page and what you want to insert directly into the email.
If you have an email template that you use for announcements or important communications, open an existing email from your inbox. Right-click on the body of the email and select View Source. A text document will open with the source code of the email message. You can use this code and insert it into the Send an email action in Power Automate.
Before you paste the code into the Send an email action, make sure you select the Code View button to change the body to HTML.
Find the area where you would normally put the content of your email. Replace the existing HTML code with the CanvasContent1 HTML data from your REST API call.
And that’s it! There are many different ways you can trigger this flow. You can set up a recurring flow that sweeps the sites each hour or at night which will send emails of from the new pages it finds. Or, you can insert an approval flow on your Site Pages library. Once the page has been approved, the announcement email will be sent. Once you have the HTML of the page, the options are endless!
Thanks for the article. But above is not working when we have web parts on page. Example i have added news web part and i am showing multiple news on it I am trying to send this page in email. Text content is coming in email but not web part content
Hi Mihir,
Unfortunately there’s lot more that makes up a news web part than static HTML which make it look the way it does on the SharePoint page (such as JavaScript and CSS). Perhaps you could use your flow to extract the text and make your own template by sending a HTML email?
Thanks for your article.
May i know if there is any way to get CanvasContent1 HTML content with CSS/Styles together? Seems the content is using CKEditor css but i cannot find the css link.
Hi Denny, thanks for reading. I’m not sure I understand your question. Can you please give additional information?
Hi there, do you know if its possible to get the existing content of a page, then using savepageasadraft to update the existing page. I know its possible using inspect page source but is there a way to do it all in power automate?
Many thanks
Hi Will, yes that’s definitely possible. I touch on a bit on it in this article: https://sharepointcass.com/2021/04/01/sharepoint-online-rest-apis-part-iii-pages/.
Basically, you will need to call the ‘Get page content’ API referenced in the above article. You can then manipulate the returned CanvasContent1 HTML by using Data Operations (https://docs.microsoft.com/en-us/power-automate/data-operations) to find and replace parts of your HTML. I know SharePoint can be quite picky about how much is changed on the page, so I would make the changes minor and not change too much of the HTML elements and structure. Then when the changes are done, you can run the ‘Save page as draft’ API with your modified HTML (it should be encoded so the HTML doesn’t interfere with the API call). Images should also be encoded in the Base64 format (this article can help with how to format images https://sharepointcass.com/2020/08/13/embedding-images-from-a-sharepoint-site-in-an-outlook-email/).
Hello!
Thanks so much for this article. 🙂 I am a Sharepoint/PowerAutomate newbie and I was wondering how you set the variable once you get CanvasContent1. Thank you!
Hi Vanessa,
Thank you for reading! You can put the output of the SharePoint HTTP request into a Compose action. For example, you can insert output(‘Send_an_HTTP_request_to_SharePoint’)?[‘body’] as an expression in Compose https://docs.microsoft.com/en-us/power-automate/data-operations
Hello!
Thanks for your article, when I try to add the above compose action I get an error. Can you please provide complete Compose action for CanvasContent1?
Hello,
Thank you for this information, it was exactly what I was looking for.
One question – How do you get around images that are embedded in the sharepoint page? Thye never seem to load in the e-mail as if the link is broken.
Thank you
hello,
i have been trying to use your code however i could not succeed.
i have a sitepage created under Site Pages folder.
when i run _api/web/lists/getbytitle(‘SitePages’)/items?$select=Title,CanvasContent1,FileLeafRef&$filter=FileLeafRef eq ‘Page-Title.aspx’
i got Action ‘Send_an_HTTP_request_to_SharePoint’ failed, and this explanation The ‘SitePages’ list does not exist on the site for the URL
also i do not any have lists in my sharepoint
appreciated if you can help
hello,
i have been trying to use your code however i could not succeed.
i have a sitepage created under Site Pages folder.
when i run _api/web/lists/getbytitle(‘SitePages’)/items?$select=Title,CanvasContent1,FileLeafRef&$filter=FileLeafRef eq ‘Page-Title.aspx’
i got Action ‘Send_an_HTTP_request_to_SharePoint’ failed, and this explanation The ‘SitePages’ list does not exist on the site for the URL
also i do not any have lists in my sharepoint
appreciated if you can help
Thank you so much, this post just save my life!
how can u get the image out from the site page?