Custom Styles for all SharePoint 2010 Rich Html Fields

I have rescued this text using the Wayback Machine at archive.org as the original blog was removed.
http://web.archive.org/web/20120701195907/http:/blog.idevteam.nl/2011/04/custom-styles-for-all-sharepoint-2010.html

Today I was asked to make only specified mark up styles available on Rich Html Fields. You can add your own mark up styles using the method mentioned in the following blog article:

http://www.sharepointblues.com/2010/10/27/custom-styles-for-sharepoint-2010-rich-html-field/

The only problem with this, is that this only works for your own page layouts, and not for the standard page layouts. As this needed to be changed for all Rich Html Fields, I had to find a solution for this. As I didn't find a ready made solution on the web, I came up with adding a custom server control to the masterpages used in the project, this control contains the following code:

[DefaultProperty("PrefixStyleSheet")]
[ToolboxData("<{0}:FixAllRichTextFields runat=server><{0}:FixAllRichTextFields>")]
public class FixAllRichTextFields : WebControl {

  [Bindable(true)]
  [Category("Appearance")]
  [DefaultValue("")]
  [Localizable(true)]
  public string PrefixStyleSheet { get; set; }

  protected override void OnLoad(EventArgs e) {
      // Only if the PrefixStyleSheet property is set, and the form is in Edit mode.
      if (!string.IsNullOrEmpty(PrefixStyleSheet) && SPContext.Current.FormContext.FormMode == SPControlMode.Edit) {
          FixRichTextFields(this.Page.Controls);
      }
  }

  /// <summary>
  /// Recursive function that checks all the controls and child controls for a RichHtmlField
  /// and changes the PrefixStyleSheet to the one set on this control.
  /// </summary>
  /// <param name="col"></param>
  private void FixRichTextFields(ControlCollection col) {
      if (col == null) return;
      foreach (Control ctrl in col) {
          if (ctrl is RichHtmlField) {
              RichHtmlField rhf = (RichHtmlField)ctrl;
              rhf.PrefixStyleSheet = PrefixStyleSheet;
          }
          else
              FixRichTextFields(ctrl.Controls);
      }
  }

}

You need to specify the PrefixStyleSheet on this control, and it will ensure that all RichHtmlFields on the page will be updated to use this setting. It will only do so when in edit mode, so there is low impact on performance. All you have to do next, is to create the css and add the CssRegistration to the masterpage, and you are done.

I hope you find this useful.

Extra content: Reaction to a comment I have just seen via archive.org

As I discovered the content of the page still existed, I also saw the following comment:

"This is great! Unfortunately it does not seem to work for the editForm.aspx"

As I recall, I did have the same problem at first. Though it is quite some time ago that I created this solution, so I could be mistaking. What I remember is that this was due to an other masterpage that is used by editForm.aspx. There is the custom master, and the default master, this control and the css should be added to both. If this isn't the case, try tweaking the if statement, like adding a check on the url containing 'editForm.aspx'.

Did a check myself, and indeed there was this problem. I tried a few things, but then I changed 'OnPreRender' to 'OnLoad' (changed the code above accordingly), this fixed the problem. I knew I had fixed it before, but I thought that that change was already reflected in this article.