For the Sherlock Web solution I had a need to be able to update rich text content in a CKEditor with rich text content from backend document templates. These backend document templates can be created and edited with the Lotus Notes client and will in some cases contain rich text content of type Rich Text (and not MIME). I can not change these backend document templates to be stored as MIME, so in order to be able to update the rich text content in a CKEditor, I thought I had to wrestle with conversion of Rich Text to MIME.
I spent a lot of time getting Rich Text to MIME conversion to work in order to add the backend document templates to the rich text content in the CKEditor. I ended up with a clumsy solution that involved saving the XPages backend document in order for the updated content to appear. I had a lot of inspiration from the answers to this Stack Overflow question on appending and prepending text to a rich text field. It worked, but…
A couple of good XPages guys suggested I took a look at using the datasource getValue() and setValue() methods instead. This turned out to be right way to go.
Part of trick is that I use the wrapDocument() function from XSnippet to get a NotesXspDocument representation of the backend document template. I can then just use getValue(“body”) on that NotesXspDocument followed by getHTML() to get a HTML representation of the content. Brilliant!
The setValue method on the inputRichText control expects MIME and not text, so I use an XPages supplied method that converts from HTML to MIME. This is necessary in order to save the document at all.
So here is the important part of my code that reads content from the target field and updates it with a backend document field contaning Notes Rich Text, and ends up saving it as MIME:
if (templateDoc != null) {
// Insert contents into existing field
if (templateDoc.hasItem("body")) {
var orgValue = document.getValue(bodyFieldName);
var origValue;
if (orgValue != null) {
// The target field already has content
origValue = ((typeof orgValue == "string")? orgValue : orgValue.getHTML()) + "";
} else {
// The target field is empty
origValue = "";
}
var templateField = wrapDocument(templateDoc).getValue("body");
importPackage(com.ibm.xsp.http);
document.setValue(bodyFieldName, com.ibm.xsp.http.MimeMultipart.fromHTML(origValue + templateField.getHTML()));
}
}
I also used an answer from Sven Hasselbach on partially refreshing contents in a CKEditor to put my inputRichText control inside a div control in order to partially refresh the div control when the above logic runs.
Using this method I am completely rid of the need to save the backend document. This just works! 🙂
—
I have added the part about updating the inputRichText control with MIME to the OpenNTF XSnippets catalog.
—
Notice: this code snippet does not work with embedded images and attachments.
Update October 2013: Here’s my solution for adding embedded images.