How to: Provision Rich Content to CEWP Programatically, the Easy Way

I have been provisioning rich text content to content editor web parts for years now and every time you have to get this done – you have to convert your special characters into encoded values otherwise the stuff won’t work.
I never asked myself if there is any other way to do it … turns out there is. Marc Irawan get’s a credit for this one … here it goes.

So the problem why you can’t have < and > in your provisioning XML is because the XML treats those as exit characters and as a result you will get a broken CEWP provisioned to your page; this means having something like this in your provisioning XML:

<WebPart…>

<Content xmlns=”http://schemas.microsoft.com/WebPart/v2/ContentEditor”>
<a href=”http://google.com”>Google</a>
</Content>

</WebPart>

… will result in a broken web part being provisioned

but renaming all the special characters in XML is not only cumbersome but also makes them unreadable later; and hard to update too!

The solution is this:

<WebPart…>

<Content xmlns=”http://schemas.microsoft.com/WebPart/v2/ContentEditor”>
<![CDATA[
<a href=”http://google.com”>Google</a>
]]]]><![CDATA[>
</Content>

</WebPart>

Try it and you’ll never look back!

For more useful tips from Yaroslav Pentsarskyy check out his free eBook on ‘Applying Branding to the Collaboration Component‘ from his book ‘Rapid SharePoint 2013 Collaboration Solution Development and Deployment’. Download Now>>

Check out Yaroslav Pentsarskyy  blog ShareMuch for more insightfull content.

Share this on...

Rate this Post:

Share:

Topics:

General