Sunday, April 19, 2020

SharePoint online - use classic rich text editor in spfx webpart


In one of my recent projects for SharePoint online, the client wanted to use the classic rich text editor in the SharePoint Framework (spfx) webpart. The one that is available by default when you edit a Multiple lines of text field in classic SharePoint experience.


The reason for this was the fact they knew how to use it from previous experience in SharePoint on-premise and the fact that all available rich text editors for modern pages had (and still do have!) many limitations.

I did some research and found out that Microsoft also uses the classic editor when editing the content of the rich text fields in modern list experience (details pane).

So I've implemented similar functionality in my webpart, which can be used on any modern or classic page for editing rich text/html.

The implemented DEMO webpart looks like this:


After clicking on the "Edit html" button, the dialog box with the editor, containing the html code opens:



You can also edit the html directly if you click the "Edit Source" button:

Implementation

In short, my approach was to:
  • use the out of the box NewForm.aspx page in dialog box, with only one rich text field; 
  • fill the field with some initial html using client side code;
  • allow the user to edit the html and click the Save button;
  • get the produced html into a javascript variable, prevent default saving and close the dialog box.
After that you can do anything with the produced html (save it in a list or library field, send it to some third-party API, save it as a property of a webpart...). My DEMO webpart saves the edited html in it's property variable.

Setup

In order for the webpart to work, you need to provide it with the title of a list and a name of the Multiple lines of text column.You can, of course, use any existing list for this, but I created a separate list (called TmpTextEditHelper), added one Multiple lines of text column (called HtmlField) and selected Enhanced rich text (Rich text with pictures, tables, and hyperlinks) as type of text. The important thing to mention is that, webpart will NEVER save anything to this list. 

The NewForm page can be opened with the following link:
https://YOURTENANT.sharepoint.com/sites/YOURSITE/Lists/TmpTextEditHelper/NewForm.aspx?OnlyIncludeOneField=HtmlField&ClientFormOverwriteSave=1&isdlg=1
  • TmpTextEditHelper - The name of the list.
  • OnlyIncludeOneField=HtmlField - Tells the server side code to render only one field (HtmlField) and exclude other fields like Title.
  • ClientFormOverwriteSave=1 - Microsoft uses this parameter also when opening the form in modern list edit pane. It probably tells the server side code not to save the form in case it is submitted, but I wasn't able to find any documentation on it.
  • isdlg=1 - Tells the server side code that the form will be viewed inside the dialog, so it does not render any unnecessary things, like navigation

Installation

After the webpart is added to AppCatalog, set as trusted and added to the SharePoint site, it can be added to any page. After adding to the page, you need to edit it's properties and set the name of the list and the field to be used, as described above.

You can find the source code for the webpart here.


3 comments:

MANOJ KUMAR said...

Thanks for the nice solution - adding classic rich text editor in mordern page .

One Issue - the iframe or dialog control is not loading. could you please let me knnow why ?

MANOJ KUMAR said...

Thanks for nice solution.

In my case dialogcontent / ifram is not loading. any reason why?

Damjan said...

Hi Manoj,
It's hard to tell like this.. but one thing you should check is if you set the name of the list and name of the field correctly.
Also check if you have any errors in the console and use that as starting point for debugging.

Sorry, but I couldn't help more here.