Custom JavaScript used to validate Knowledge Base article content before submission is stripping all rich text formatting. We’re using the rich text control API to get the content, run validation checks, and set it back if changes are needed.
The validation logic works fine, but after our script runs, all formatting (bold, italics, bullet points, tables) disappears and we’re left with plain text. Looking at the getContent and setContent methods in our code:
var content = Xrm.Page.getControl("description").getValue();
if (validateContent(content)) {
Xrm.Page.getControl("description").setValue(content);
}
The HTML formatting preservation seems to be the issue. Has anyone worked with the rich text control API in D365 9.1 and maintained formatting through JavaScript manipulation?
The root cause is your approach to rich text control API usage and how getContent/setContent methods handle HTML formatting preservation.
For D365 9.1 Knowledge Base rich text fields, you must use the attribute-level methods, not control-level methods:
var content = Xrm.Page.getAttribute("description").getValue();
if (validateContent(content)) {
// Only call setValue if you actually modified content
Xrm.Page.getAttribute("description").setValue(content);
}
The key issue in your original code: you’re calling setValue even when content hasn’t changed. Every setValue call on a rich text control triggers a re-render that can strip formatting if not done correctly.
Better approach - only set if you modify:
var originalContent = Xrm.Page.getAttribute("description").getValue();
var validatedContent = validateAndClean(originalContent);
if (validatedContent !== originalContent) {
Xrm.Page.getAttribute("description").setValue(validatedContent);
}
Also critical: ensure your validateContent function doesn’t decode HTML entities or strip tags. If you’re using regex or string manipulation, you might be inadvertently removing HTML. Use a DOM parser if you need to analyze HTML structure while preserving formatting.
For form save event handling, register your validation on PreSave instead of OnChange. This prevents the event loop issue where setValue triggers OnChange, which calls your validation again, creating a cycle that degrades the HTML content.
Finally, verify the rich text control configuration in the form editor. If it’s set to “Plain Text” format instead of “Rich Text”, no API method will preserve formatting. Check Form Editor > Field Properties > Formatting tab.
This draft is based on general Microsoft Dynamics 365 Sales knowledge. It has not been verified against your specific version and environment. Practitioners: verify the steps and share your experience below.
Your code is using the wrong methods for rich text controls. getValue() returns plain text only. For rich text fields in D365 9.1, you need to use getAttribute().getValue() to get the HTML content, then setAttribute().setValue() to set it back. The control-level getValue/setValue methods don’t preserve HTML markup.
Actually, even getAttribute().getValue() can have issues with rich text controls depending on the control type. The Knowledge Base description field uses a specific rich text editor control. You should be accessing it through the control’s data value property, not directly through getValue(). Also make sure you’re not accidentally encoding the HTML - if your validation function converts entities or escapes characters, that’ll break the formatting when you set it back.
I tried switching to getAttribute().getValue() but still seeing the same issue. The validation function doesn’t modify the content, just checks word count and for prohibited terms. When I log the content to console before and after, the HTML tags are there, but they’re not rendering in the control after setValue.
Check if your validation is running on the OnChange event. Rich text controls in D365 9.1 have a quirk where if you setValue during OnChange, it triggers another OnChange event that can clear formatting. You might be creating an event loop that eventually strips the HTML. Try moving your validation to a different event like OnSave instead, or add a flag to prevent recursive calls.
The issue is how you’re interacting with the rich text control. In D365 9.1, these controls have specific methods for HTML content. Instead of using getValue/setValue on the control, you need to work directly with the attribute and ensure you’re not triggering unnecessary control updates. Also, if you’re validating and not actually changing the content, there’s no need to call setValue at all - that’s what’s causing the formatting loss.