Controls - How to convert HTML to Text

A question that arises frequently is, how do I convert HTML to text, or how do I display rich text content through a Power Apps screen? This post describes the main techniques that we can use.

Example scenario

To demonstrate the typical use case scenario, let's take a SharePoint list of issues. This list contains an issue description column that is set up as a 'multiple lines of text' column. The 'enhanced rich text' setting is enabled for this column.

With this list, users have entered records directly through SharePoint. The screenshot here illustrates an example of a richly formatted record.



If we now create a form that's based on this SharePoint list, here's how the default display form looks. The form displays the HTML markup, rather than the properly formatted text. This can be particularly confusing for app builders who are unaware that what they see here is HTML.

How to display SharePoint rich text values on a form

The easiest and correct way to display this data on a form is to use the 'View rich text' control. We can configure a card to use this control by clicking Field > Edit, and changing the control type from 'View text' to 'View rich text'.


The same method applies on an edit form. We would change the control type to 'Edit rich text'.

How to strip HTML characters / convert rich text to plain text

In the case where we want to strip the rich text formatting and to display the plain text only, we can call the PlainText function.

This can be very helpful in cases where input values come from external sources such as RSS feeds or social media, and we want to strip all extrenuous formatting.

The PlainText function accepts a single value (an HTML or XML string), and the return value from this function is the plain text equivalent. The screenshot here illustrates the use of this function.


How to convert HTML to text using the Content Conversion connector

In most cases, the PlainText function is the best way to perform XML/HTML text conversions. For completeness, another way to convert HTML to text is to use the "Content Conversion" connector. This connector exposes a single method called HtmlToText.


The key difference between this and the PlainText function is that HtmlToText is a behavior function. This effectively means that we can call it from a button, but not apply it directly to the text property of a control.

A example use for this connector could be an app that retrieves HTML input from a data source, applies some logic to process this input , and passes the output to a Flow or some other data source.

To use this method, we first add the "Content Conversion" connector to our app.


Here's an example of the formula that we can add to the OnSelect property of a button to convert static a piece of HTML into text, and to store the result in a variable.

Set(varPlainText,
ContentConversion.HtmlToText("<div>Example HTML Input</div>")
)

Conclusion

A common requirement is to display rich text SharePoint content on a form, or to convert HTML to text. This post summarised the techniques we can use to perform these tasks.

Related posts

FormuIas - Is it possible to call a user-defined function recursively in Power Apps?
January 31, 2024
Formulas - A beginners guide on how to create and call user-defined functions (UDFs)
January 28, 2024
Formula - How to add a button that converts degrees Centigrade to Fahrenheit and vice versa
May 08, 2023
Formula - How to convert a single delimited string to rows and columns
April 05, 2023
Data - How to group data in a gallery and calculate sums
January 20, 2023
Formula - How to calculate compound interest
November 24, 2022
Utilities - The best way to peform OCR on images of Power Apps Formulas
October 11, 2022
Example - How to use a drop down control to convert currencies
September 27, 2022
Formula - How to parse JSON in Power Apps- 4 examples
September 15, 2022
Data - How to get a row by ordinal number
April 28, 2022
Formula - What to do when the If statement doesn't work?
December 17, 2021
Formula - Boolean And / Or operators - What is the order of precedence?
December 16, 2021
Controls - How to set the data source of a Combo Box to a comma separated string
November 16, 2021
Numbers - 10 examples of how to round numbers
August 18, 2021
Formula - Difference between round, square, and curly brackets
July 20, 2021
Top 3 highlights of upcoming enhancements to the Power Apps language (Power FX)
May 26, 2021
Email - Sending email attachments with the Office 365 Outlook connector
March 30, 2021
Formula - What to try when numbers don't format correctly
March 24, 2021
Formulas - how to return all days between two dates
March 15, 2021
Formula - How to create comma separated (CSV) list of items
February 25, 2021
Formula - How to use the IF and Switch functions - 3 common examples
February 12, 2021
Location - Finding the closest location and and sorting records by distance, based on the current location of the user
January 24, 2021
Formulas - How to cope with weekends and public holidays in date calculations
January 21, 2021