Configuring SharePoint 2010 Rich Text Editor to allow ‘Paste plaintext’ only


Recently I wrote an article how you can configure the SharePoint 2010 Rich Text Editor (RTE) to better support consistent content authoring. Shortly after I published the article, I got a question how to force the RTE to allow the content editors to paste content only as plain text removing all formatting. From the consistency point of view this is a great deal especially if RTE is not your primary text editor and you’re pasting the content from another text editor. So how do you configure the SharePoint 2010 Rich Text Editor to allow pasting content only as plain text?

As you have learned from my previous article on configuring the SharePoint 2010 Rich Text Editor, the new RTE has some impressive extensibility capabilities. By leveraging those standard capabilities you can tailor the content authoring experience to fit your requirements. The best of it all is that a big part of it can be done without the involvement of custom code. One of such customization examples is configuring the paste options for the Rich Text Editor.

SharePoint 2010 Rich Text Editor Paste options highlighted on the Ribbon.

SharePoint 2010 Rich Text Editor ships with two options for pasting rich text: you can either paste it as-is keeping all the formatting or you can paste it as text. If the text contains any formatting it will get removed and only plain text will be pasted. From the code point of view both paste options are very similar: they call the RTE.Cursor.paste function and the only difference is the value of the boolean asText parameter.

If you’re interested in allowing pasting as plain text only, it’s probably the easiest to assign the Paste as text functionality to the regular Paste button:

RTE.RichTextEditor.paste = function() { RTE.Cursor.paste(true); }

Pasting using the Ribbon Paste button is not the only way to paste content in SharePoint 2010. As in every content editor you can also use the CTRL+V hotkey to paste content from the clipboard. The challenging part is that pasting using the CTRL+V key combination uses a slightly different approach than the Paste button making it a little more challenging to cover both cases. After some testing I’ve found that you can easily cover both cases by adding the following flag to the previous code snippet:

RTE.Cursor.$3C_0 = true;

This line of JavaScript will set the internal flag that will let SharePoint know that all content should be pasted as text. As you can see it uses internal API of the SharePoint 2010 Rich Text Editor so it’s definitely something you want to keep in mind to test after applying future updates.

Wrapping it all together the following code snippet is the only thing that you need to configure the SharePoint 2010 Rich Text Editor to paste all contents as plain text:

if (RTE) {
    RTE.RichTextEditor.paste = function() { RTE.Cursor.paste(true); }
    RTE.Cursor.$3C_0 = true;
}

The first line, that I didn’t mention before, allows you to ensure that the RTE JavaScript has been loaded and so you can avoid JavaScript errors.

Depending on your requirements you can include the above code snippet in a Custom Action activated either on the Site Collection level or on a specific Site, or you can include it as a part of a Page Layout if you want to disallow pasting rich content on specific pages only.

Technorati Tags: SharePoint 2010

Others found also helpful: