To convert a plain string to HTML in Power Automate, you typically use the compose action with an HTML template and the concat() function to wrap your string in HTML tags, or you use the html() function in expressions to treat a string as HTML content. The exact method depends on whether you need to display the string in an email, a SharePoint page, or a custom connector. This article explains the core techniques, common pitfalls, and practical examples for turning plain text into formatted HTML within your flows.
Why Convert a String to HTML in Power Automate?
Power Automate often handles plain text strings—from form responses, database fields, or user input. When you need to send that text in an email, create a SharePoint page, or generate a PDF, you often want formatting: bold headings, bullet points, or clickable links. HTML is the standard markup language for such formatting. Converting a string to HTML lets you embed that text into an HTML email body or a web page with the structure you want. Without conversion, the text appears as a single block with no styling.
For example, a Microsoft Forms response might contain a multi-line comment. When you send it in an email using the default Send an email action, the text appears as plain text. By converting it to HTML, you can preserve line breaks, add emphasis, or insert it into a styled email template.
Using the compose Action with HTML Tags
The most straightforward way to convert a string to HTML is to use the Compose action to wrap the string in HTML tags. This is useful when you have a fixed template and want to insert your dynamic string into it.
- Add a Compose action to your flow.
- In the Inputs field, enter an expression that concatenates your HTML tags with the string. For example, if your string is in a variable named
myString, use:concat('<p>', variables('myString'), '</p>') - Use the output of the Compose action wherever you need the HTML—for example, in the Body of an email action set to HTML format.
This method works for simple wrapping. For more complex transformations, such as converting line breaks to <br> tags, you need a more advanced expression.
Using the html() Function in Expressions
The html() function in Power Automate expressions tells the system to treat a string as HTML content. This is particularly useful when you want to embed a dynamic string inside an HTML email body without manually escaping characters.
For example, in an email action, set the Body field to HTML and use an expression like:
html(outputs('Compose_myString'))
This ensures that any HTML tags in the string are rendered, not shown as plain text. However, be cautious: if the string contains untrusted user input, this could allow HTML injection. Always sanitize or encode if the input is not from a trusted source.
Converting Line Breaks to HTML
Tags
A common need is to convert plain text with newlines into HTML with <br> tags. Power Automate does not have a built-in function for this, but you can use the replace() function to substitute newline characters.
In an expression, you can replace the newline character (represented as \n in some contexts) with <br>. For example:
replace(variables('myString'), '\n', '<br>')
Note that in Power Automate expressions, the newline character is often represented as \n (backslash-n) in the expression editor. However, the actual character might be a carriage return and line feed (\r\n) depending on the source. To handle both, you might need to replace \r\n first, then \n. A more robust expression:
replace(replace(variables('myString'), '\r\n', '<br>'), '\n', '<br>')
This ensures that Windows-style line breaks are converted correctly.
Using HTML Tables and Lists
If your string contains structured data, such as a list of items, you might want to convert it into an HTML list or table. For example, if you have a string like “Apple, Banana, Cherry”, you can split it and generate an unordered list.
Power Automate does not have a direct loop to build HTML, but you can use the join() function with a split() to create list items. For a comma-separated string in variable myList:
concat('<ul><li>', join(split(variables('myList'), ','), '</li><li>'), '</li></ul>')
This yields an HTML unordered list. For tables, you’d need to use an Apply to each loop to construct rows, which is more complex but doable.
Common Pitfalls and How to Avoid Them
Several issues frequently arise when working with string-to-HTML conversions:
- Escaping characters: When you type HTML tags in expressions, you must escape the angle brackets using
<and>in the expression editor, or use theconcat()function with single-quoted strings. Failing to do so results in syntax errors. - Encoding of special characters: If your string contains ampersands (&), quotes, or other special characters, they may need to be HTML-encoded to display correctly. Use the
encodeUriComponent()function if you need to encode for URLs, but for HTML content, you might need to replace&with&manually. - Email body format: When sending an email, ensure the email action’s Is HTML toggle is set to Yes. Otherwise, the HTML tags will appear as plain text.
- Null or empty strings: If the string is empty, your HTML output might contain empty tags. Use a
if()condition to handle empty strings gracefully.
Alternatives: Using HTML in SharePoint or Dataverse
If you’re working with SharePoint or Dataverse, you might not need to convert strings manually. For example, SharePoint columns can be set to allow rich text, and you can use the Create item action to insert HTML directly into a multiline text field set to HTML mode. Similarly, Dataverse has a text format option that supports HTML. In such cases, you can pass the string directly without conversion, as the system handles the HTML rendering.
However, when you need to generate an email body or a PDF from a string, conversion is necessary. For PDFs, you might use a connector like Encodian or Plumsail, which often accept HTML templates and convert them to PDF. These connectors provide built-in functions to merge strings into HTML templates, saving you from manual concatenation.
Frequently Asked Questions
How do I convert a string to HTML in Power Automate?
Use the compose action with a concat() expression to wrap your string in HTML tags, or use the html() function in an expression to treat the string as HTML content. For line breaks, use the replace() function to convert newline characters to <br> tags.
Why is my HTML showing as plain text in the email?
This usually happens because the email action’s Is HTML option is set to No. In the Send an email action, expand the Advanced options and set Is HTML to Yes. Also, ensure your HTML string is properly formed.
Can I convert a string with bullet points to an HTML list?
Yes, you can split the string by a delimiter (like a newline or comma) and use the join() function to create list items. For example, concat('<ul><li>', join(split(variables('myString'), '\n'), '</li><li>'), '</li></ul>') converts a newline-separated string into an unordered list.
How do I handle special characters like & in my string?
In HTML, the ampersand must be written as & to display correctly. You can use the replace() function to replace & with & in your string before inserting it into HTML. For example, replace(variables('myString'), '&', '&').
Is there a way to convert a string to HTML without using expressions?
If you are using a third-party connector like Plumsail or Encodian, they often provide a HTML to PDF or Merge HTML action that accepts a template and a string, handling the conversion for you. These connectors typically have a user-friendly interface to map your string into placeholders.
Can I convert a string to HTML in a SharePoint list column?
Yes, if the SharePoint column is a multiline text column set to Enhanced rich text or Plain text, you can use the Create item or Update item action to pass an HTML string. However, the column must be configured to allow HTML; otherwise, it will display as plain text.
Related
- Random Number 1-30: How to Generate One
- Conditional Format Blank Cells: Easy Excel Guide
- How to Select All Words on a Laptop (Windows & Mac)
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Delete Duplicate Data in ExcelTo delete duplicate data in Excel, select a cell in your list, go to Data > Remove Duplicates,…
- Excel Shortcut Desk Mat: What It Is and How to ChooseAn Excel shortcut desk mat is a large mouse-and-keyboard surface printed with keyboard commands for Microsoft Excel. It…
- How to Flip a Column Upside Down in Excel: A Quick GuideTo flip a column upside down in Excel, add a temporary index beside the data, sort the entire…
- AG Grid Hide Column Menu: How to Use and Customize ItTo hide a column in AG Grid, open the column’s header menu and choose its hide option, if…
- Classic Outlook Block Sender: Setup and TroubleshootingTo block a sender in classic Outlook for Windows, select one of their messages, then choose Home >…
- Block a Sender in Outlook Permanently: What Actually WorksTo block a sender in Outlook and keep the block in place, add the sender’s email address to…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.