SharePoint Experts, Information Architects, Expert Witness

We provide consulting in a broad array of business and technology from architecture to design to deployment of global systems with a focus on surfacing data in the enterprise. Specialists in Microsoft, we are a premier provider of SharePoint Expertise (including 2016 and Office 365). We also provide Expert Witness/Legal Expert in eDiscovery, source discovery, patent infringement, piracy and more! We also have established SICG DLDS s.a. - our counterpart in Costa Rica that specializes in water systems (http://www.crwatersolutions.com) - Contact me direct: david_sterling@sterling-consulting.com or call 704-873-8846 x704.

Search This Blog

Wednesday, August 5, 2009

Adding the Rich Text/HTML Editor control to a Web Part

You might have tried this but had limited success (see warning below) however it is really quite easy to add this control to a web part...just do the following:

In your Web Part project, add a reference to the SharePoint Publishing DLL - in your project, right click on References then under the .NET tab, locate "Microsoft COntent Publishing and Management"

Next, add the using statements in the code:

using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.WebControls;

Next, define the control you want to create:

Microsoft.SharePoint.Publishing.WebControls.HtmlEditor DescriptionEditorField = new HtmlEditor();

(note because of the Using, you don't need the full namespace but I showed it here for reference)


Next, in the OnLoad method, define and add the control to the web part:

//
//-----------------------------
// DescriptionEditorField
//-----------------------------
//
DescriptionEditorField.ID = "DescriptionEditorField";
DescriptionEditorField.ToolTip = "Description Field";
DescriptionEditorField.PopupEditorMode = true;
DescriptionEditorField.AllowHtmlSourceEditing = true;
DescriptionEditorField.UseIntranetMode = true;
DescriptionEditorField.DisableCustomStyles = false;
DescriptionEditorField.DisableBasicFormattingButtons = false;
DescriptionEditorField.EnableExtensions = false;
DescriptionEditorField.DefaultAssetLocation = "/";
DescriptionEditorField.DefaultAssetImageLocation = "/";
//
this.Controls.Add(DescriptionEditorField);
//

NOTE 1: You MUST use the Popup mode for this to work correcting in a Web Part! If set to "false", it will display but you will not be able to edit.

NOTE 2: For this to work correctly, be SURE that base.Render(writer); is NOT in the Render method (if it is, the control will render twice!)

In the Render method, you can render the control as:

DescriptionEditorField.RenderControl(writer);

WARNING:

When using certain controls (like the Menu Control), it may require that HtmlEditor controls BE RENDERED EVEN IF NOT USED. The effect if you do not, is that you get a JavaScript error on the page indicating it cannot find the control. To prevent this, you can simply check to determine if you want to render or not and if not, set the visibility to false:

DescriptionEditorField.Visible = false;


Happy Coding!!!