SharePoint displaying HTML tags when rendering Enhanced Rich Text field

Posted by & filed under , .

I came across an issue where a “Multiline Text Field”, originally created as “Plain Text” but later updated to be “Enhanced Rich Text” was improperly rendering in Display Mode by showing the HTML tags instead of rendering them (in other words, it was escaping the HTML, rather than rendering it). I found one post on technet that discussed the issue, which seemed to indicate that it was an issue with the RichHtml configuration of the fields.

In my case, I created a Site Column, and then a Site Content Type, and then added that content type to the list. After doing all of this, I changed the Site Column format to allow for “Enhanced Rich Text”, which appears to have updated the “RichTextMode” to be “FullHtml” on the list, but for some reason the “RichText” property did not get carried through to the version of the column attached to the Lists.

I wrote some JSOM that can be run in the dev console to easily set the RichText to “TRUE” by providing the list and column names. The “whatIf” property allows you to easily check the current value of the schema, as well as what change would be made to the schema if you set that property to “false”.

 

var whatIf = true;
var listTitle = "Pages";
var columnTitle = "My Column Name";

var context = new SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle(listTitle);
var field = list.get_fields().getByTitle(columnTitle); 
context.load(field);
context.executeQueryAsync(function () {
    var schema = field.get_schemaXml();
    if(schema.indexOf('RichText="FALSE"') > 0){
        var modifiedSchema = schema.replace('RichText="FALSE"', 'RichText="TRUE"');
        console.log("RichText set to false on field '"+columnTitle+"'");
        if(whatIf){
            console.log("'whatIf' mode currently active, schema will not be updated, but if 'whatIf' is set to false, the schema will be updated as follows.");
            console.log("Current schema:");
            console.log(schema);
            console.log("New schema:");
            console.log(modifiedSchema);
        }else{
            field.set_schemaXml(modifiedSchema);
            field.update();
            context.executeQueryAsync(function () {
                console.log("Schema on field '"+columnTitle+"' updated on list '"+listTitle+"' to have RichText set to 'TRUE'. New schema: ");
                console.log(field.get_schemaXml());
            }, function (sender, args) {
                console.error('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            });
        }
    }else{
        console.log(schema);
        console.log("'RichText=\"FALSE\"' not found in schema, aborting");
    }
}, function (sender, args) {
    console.error('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
});

After running this script, the SharePointWebControls:RichTextField now property renders my “Enhanced Rich Text” content without escaping the HTML.