Recently I had a requirement for a client to convert their existing site over to Power Pages. One of the items I needed to convert was a "Recent Announcements" page. This is a pretty straightforward page with only a title of "Recent Announcements" and a grid showing the announcements. The grid only has two columns, 'Created On' and 'Message'. The problem I had to solve was I used the Rich Text Editor control on the 'Message' field. This means that raw HTML was stored in the field. Out of the box Power Pages can't render the raw HTML in the column. This means in the Power Page would have raw HTML showing instead of a nicely formatted message. This is easily solved with a small JavaScript function.
The first thing I did was create a custom entity in DataVerse called Announcement. Here is the layout of the fields:
- Changed the primary name field from 'Name' to 'Subject'.
- Added a multi-line text field called 'Message'.
- Add a two-option set field called 'Show in Portal'.
- This is used to control whether to show the announcement in the portal or not. This way they can have draft announcements. I know there are other ways of doing it with Status and Status Reason. This was just the approach I took.
Since we are only showing a view in Power Pages, there was no need for me to setup a form for Power Pages. Instead, the form I setup is for the Model Driven App in DataVerse only. Once I setup the form, I set the multi-line text field control to Rich Text Editor. Now users can format the text any way they see fit for the announcement. This is something they didn't have in the existing site.
Next, I created a view called 'Live Announcements' This view has 2 filters:
- Status of Active.
- Show on Portal set to Yes.
This view will be used to show the messages in the portal and will give users in the Model Driven App a quick reference of what is showing in Power Pages and what isn't.
At this point we setup the Page. I won't go through step by step how to do that in this post as the new Power Pages process is really easy for creating a new page and adding a list to it. Just remember to have table permissions setup. In my case the table permissions are a simple Global Read Only permission on the announcement table.
At this point you should see your list on your Power Page. But you will notice that the message column contains raw HTML. Here is how to fix that easily.
- Open up the 'List' Record in DataVerse and click on the 'Options' tab.
- In the 'Custom JavaScript' field input the below JavaScript
1: $(document).ready(function (){
2: $(".entitylist.entity-grid").on("loaded", function () {
3: $('td[data-attribute="REPLACE WITH FIELD SCHEMA NAME"]').each(function() {
4: var rawHtml = $(this).attr('data-value');
5: $(this).html(rawHtml);
6: });
7: });
8: });
Here is what this JavaScript does. When the document is ready it checks for a loaded event from the list (grid). If you don't do this, check the code won't work as the page is loaded asynchronously and the grid might not be loaded by the time the read function runs. By running the on function, looking for a loaded response you will trigger the remaining code when the grid is ready. Once the grid is ready, we will get our column that contains the raw html. Make sure to replace the 'REPLACE WITH FIELD SCHEMA NAME' text with the schema name of your column (schema name of the DataVerse field). For each row in that column, we will read the raw HTML that the Rich Text Editor generated and render it.
Make sure to save your changes and sync your configuration. Once that is done clear your cache and check the list. You should no longer see raw html but nicely formatted text, the way the user wanted it to be.
Comments
Post a Comment