Posts

Posts uit februari, 2018 tonen

Unwanted HTML elements in your Sitecore richtext fields

Custom save events to the rescue! I have been planning this blog for years maybe but kept postponing, but finally it is here after similar requests from our great Sitecore community. The problem I have solved in my projects in the past is the following: When editing rich text fields, the default behavior of the Sitecore Telerik control is to insert a bit too many HTML elements around your content. For example <p> tags you don't want there, <p> tags or divs around image that you embed in the rich text field etc.  At some point I was so annoyed by this and the bad HTML it produced, I decided to customize the item save event to clean up the resulting HTML. Below you will find my code, I hope you find it useful. Be aware this code has only been tested on Sitecore 7.x. While I expect it to also work for 8+, you better test for yourself whether nothing breaks. Also, extend to your own need, to create your own neat and clean HTML. /// /// Don't forget follo