
Out of the box, SharePoint offers different editors for different types of data. For instance, a string property is represented by the text box editor, while a Boolean property is represented by check boxes. In this section, you will learn how to create custom editor parts, which will be shown in the web part tool pane when you are configuring a web part.
All editor parts inherit from the EditorPart base class, so that is the first step you need to take when creating a custom editor part. Editor parts look a lot like normal web parts. For instance, if you want to add child controls to an editor part, you need to override its CreateChildControls() method. The following code fragment adds a text area consisting of five lines to an editor part:
protected override void CreateChildControls()
{
You will also need to override the ApplyChanges() method of an EditorPart instance. This
method is responsible for mapping values in the editor part to corresponding properties in the
associated web part. The following code fragment shows how the ApplyChanges() method sets a
property in an instance of the MyWebPart web part called NormalValue:
public override bool ApplyChanges()
{
The final piece of an editor part that must be implemented is the SyncChanges() method. This method is the opposite of the ApplyChanges() method; it retrieves property values from a web part and stores them in the editor part. The following code fragment shows an implementation for the SyncChanges() method that retrieves the NormalValue property of a MyWebPart web part and stores it in the text area of the web part editor tool pane:{
_txtNormalBox = new TextBox();
_txtNormalBox.ID = “txtNormalBox”;
_txtNormalBox.Text = “[Custom editor part]”;
_txtNormalBox.TextMode = TextBoxMode.MultiLine;
_txtNormalBox.Rows = 5;
Controls.Add(_txtNormalBox);
}_txtNormalBox.ID = “txtNormalBox”;
_txtNormalBox.Text = “[Custom editor part]”;
_txtNormalBox.TextMode = TextBoxMode.MultiLine;
_txtNormalBox.Rows = 5;
Controls.Add(_txtNormalBox);
You will also need to override the ApplyChanges() method of an EditorPart instance. This
method is responsible for mapping values in the editor part to corresponding properties in the
associated web part. The following code fragment shows how the ApplyChanges() method sets a
property in an instance of the MyWebPart web part called NormalValue:
public override bool ApplyChanges()
{
MyWebPart objMyWebPart = (MyWebPart)WebPartToEdit;
objMyWebPart.NormalValue = _txtNormalBox.Text;
return true;
}objMyWebPart.NormalValue = _txtNormalBox.Text;
return true;
public override void SyncChanges()
{
// Make sure the text area child control is created.
EnsureChildControls();
MyWebPart objMyWebPart = (MyWebPart ) WebPartToEdit;
_txtNormalBox.Text = objMyWebPart.NormalValue;
}EnsureChildControls();
MyWebPart objMyWebPart = (MyWebPart ) WebPartToEdit;
_txtNormalBox.Text = objMyWebPart.NormalValue;
So far, you have seen how to create an editor part. Before a custom editor part is shown in the web part editor tool pane, you need to implement support for this in your web part. In this example, we are creating a web part called MyWebPart. First, we need to add one or more properties that are read from and written to within the editor part. The custom editor part in this section expects the
presence of a NormalValue property in a web part, so that is the first thing that needs to be implemented.
The following code snippet shows how to implement the NormalValue property:
private string _strNormalValue = String.Empty;
[Personalizable(PersonalizationScope.Shared), WebBrowsable(false), WebDisplayName(“Normal value”), WebDescription(“Normal value description”)]
public string NormalValue
{
After that, you need to override the CreateEditorParts() method. This method is responsible for returning a collection of custom editor part controls that are shown in the web part editor tool pane when a web part is in edit mode. Basically, this method is used to create a new instance of one or more custom editor parts and add those to the editor part collection. This collection is used[Personalizable(PersonalizationScope.Shared), WebBrowsable(false), WebDisplayName(“Normal value”), WebDescription(“Normal value description”)]
public string NormalValue
{
get { return _strNormalValue; }
set { _strNormalValue = value; }
}set { _strNormalValue = value; }
when the web part tool pane is rendered. The following code fragment shows how to add the MyWebPartEditorPart editor part to the collection of customer editor parts that is shown when the MyWebPart web part is being edited:
public override EditorPartCollection CreateEditorParts()
{
and here is the complete code:{
MyWebPartEditorPart objEditor = new MyWebPartEditorPart ();
objEditor.ID = ID + “normalEditor1”;
objEditor.Title = “Normal Editor title”;
objEditor.ToolTip = “Normal Editor tooltip”;
objEditor.TabIndex = 100;
objEditor.GroupingText = “Normal editor grouping text”;
ArrayList objEditorParts = new ArrayList();
objEditorParts.Add(objEditor);
EditorPartCollection objEditorPartsCollection = new
EditorPartCollection(objEditorParts);
return objEditorPartsCollection;
}objEditor.ID = ID + “normalEditor1”;
objEditor.Title = “Normal Editor title”;
objEditor.ToolTip = “Normal Editor tooltip”;
objEditor.TabIndex = 100;
objEditor.GroupingText = “Normal editor grouping text”;
ArrayList objEditorParts = new ArrayList();
objEditorParts.Add(objEditor);
EditorPartCollection objEditorPartsCollection = new
EditorPartCollection(objEditorParts);
return objEditorPartsCollection;
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
namespace KwikSliver.Sharepoint.WebpartDemo
{
public class MyWebPartEditorPart : EditorPart
{
TextBox _txtNormalBox;
protected override void CreateChildControls()
{
_txtNormalBox = new TextBox();
_txtNormalBox.ID = “txtNormalBox”;
_txtNormalBox.Text = “[Custom editor part]”;
_txtNormalBox.TextMode = TextBoxMode.MultiLine;
_txtNormalBox.Rows = 5;
Controls.Add(_txtNormalBox);
}
public override bool ApplyChanges()
{
MyWebPart objMyWebPart = (MyWebPart )WebPartToEdit;
objMyWebPart.NormalValue = _txtNormalBox.Text;
return true;
}
public override void SyncChanges()
{
EnsureChildControls();
MyWebPart objMyWebPart = (MyWebPart)WebPartToEdit;
_txtNormalBox.Text = objMyWebPart .NormalValue;
}
}
}{
TextBox _txtNormalBox;
protected override void CreateChildControls()
{
_txtNormalBox = new TextBox();
_txtNormalBox.ID = “txtNormalBox”;
_txtNormalBox.Text = “[Custom editor part]”;
_txtNormalBox.TextMode = TextBoxMode.MultiLine;
_txtNormalBox.Rows = 5;
Controls.Add(_txtNormalBox);
}
public override bool ApplyChanges()
{
MyWebPart objMyWebPart = (MyWebPart )WebPartToEdit;
objMyWebPart.NormalValue = _txtNormalBox.Text;
return true;
}
public override void SyncChanges()
{
EnsureChildControls();
MyWebPart objMyWebPart = (MyWebPart)WebPartToEdit;
_txtNormalBox.Text = objMyWebPart .NormalValue;
}
}
and below shown is the code for implementation of the MyWebPart web part. This web part displays the custom MyWebPartEditorPart web part in edit mode:
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
namespace KwikSilver.Sharepoint.WebpartDemo
{
[Guid(“288802c4-4dfe-45b6-bb28-49dda89ec225”)]
public class MyWeblPart : System.Web.UI.WebControls.WebParts.WebPart
{
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write(“Normalvalue: “ + NormalValue);
}
public override EditorPartCollection CreateEditorParts()
{
MyWeblPartEditorPart objEditor = new MyWeblPartEditorPart ();
objEditor.ID = ID + “normalEditor1”;
objEditor.Title = “Normal Editor title”;
objEditor.ToolTip = “Normal Editor tooltip”;
objEditor.TabIndex = 100;
objEditor.GroupingText = “Normal editor grouping text”;
ArrayList objEditorParts = new ArrayList();
objEditorParts.Add(objEditor);
EditorPartCollection objEditorPartsCollection = new EditorPartCollection(objEditorParts);
return objEditorPartsCollection;
}
private string _strNormalValue = String.Empty;
[Personalizable(PersonalizationScope.Shared), WebBrowsable(false), WebDisplayName(“Normal value”), WebDescription(“Normal value description”)]
public string NormalValue
{
get { return _strNormalValue; }
set { _strNormalValue = value; }
}
}{
writer.Write(“Normalvalue: “ + NormalValue);
}
public override EditorPartCollection CreateEditorParts()
{
MyWeblPartEditorPart objEditor = new MyWeblPartEditorPart ();
objEditor.ID = ID + “normalEditor1”;
objEditor.Title = “Normal Editor title”;
objEditor.ToolTip = “Normal Editor tooltip”;
objEditor.TabIndex = 100;
objEditor.GroupingText = “Normal editor grouping text”;
ArrayList objEditorParts = new ArrayList();
objEditorParts.Add(objEditor);
EditorPartCollection objEditorPartsCollection = new EditorPartCollection(objEditorParts);
return objEditorPartsCollection;
}
private string _strNormalValue = String.Empty;
[Personalizable(PersonalizationScope.Shared), WebBrowsable(false), WebDisplayName(“Normal value”), WebDescription(“Normal value description”)]
public string NormalValue
{
get { return _strNormalValue; }
set { _strNormalValue = value; }
}
}
and here is the end result for all the efforts made above!..


