.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Creating a Custom Editor Part

Posted By:Manning       Posted Date: February 19, 2011    Points: 75    Category: SharePoint    URL: http://www.dotnetspark.com  

Creating a Custom Editor Part.In this article the author discusses the steps involved with creating a custom Editor Part in SharePoint 2010.

This article is taken from the book SharePoint 2010 Web Parts in ActionThe author discusses the steps involved with creating a custom Editor Part.

Get 40% off any version of SharePoint 2010 Web Parts in Action with the checkout code dnspark40. Offer is only valid through www.manning.com.

In our RSS Web Part, we have two properties that are simple input values and they work as they should without validation, but the user could write basically anything in those text boxes. The feed URL should be validated so that it is a correct URL and the number of items to show should always be an integer equal to or larger than one.

Adding a new class

To create a custom Editor Part, the first thing we have to do is to add a new class to our Web Part item. Right-click the Web Part item and select Add > Class. Enter the name RssWebPartEditorPart as the name of the class.
To create the EditorPart, you need to ensure that your file inherits from the base EditorPart class,
defined in the System.Web.UI.WebControls.WebParts namespace. To ineherit from the EditorPart, you edit the class definition like this:

class RssWebPartEditorPart: EditorPart

Creating the user interface

The user interface of the Editor Part is built by creating the input fields in the CreateChildControls method, as seen in listing 1.

 Listing 1 Creation of the controls in the Editor Part  

protected TextBox m_feedUrl;	#1 protected TextBox m_itemCount;	#1

protected override void CreateChildControls() {
m_feedUrl = new TextBox(){ID="feedUrl"};	#2 m_itemCount = new TextBox() {ID="itemCount"};	#2

this.Controls.Add(new LiteralControl("Feed Url:
")); #3 this.Controls.Add(m_feedUrl); #3 this.Controls.Add(new LiteralControl("
Items to show:
")); #3 this.Controls.Add(m_itemCount); #3 this.Title = "Feed Properties"; #4 base.CreateChildControls(); this.ChildControlsCreated = true; }

The input fields are defined as protected (#1) and created in CreateChildControls (#2). The controls are then added to the child controls collection (#3). The Title property is set (#4) to the title of the Editor Part. This is especially important if you have several Editor Parts.

Populating the controls in Editor Part

To read information from the Web Part that is being edited the Editor Part has a method called SyncChanges. You have to override this method and read the property values from the Web Part and then set the values of the local
Editor Part controls as in listing 2.

 Listing 2 The SyncChanges method that populates the controls in the Editor Part  

public override void SyncChanges() { EnsureChildControls();
RssWebPart webPart = this.WebPartToEdit as RssWebPart;	#1 if (webPart != null) {
m_feedUrl.Text= webPart.RssFeedUrl;	#2 m_itemCount.Text = webPart.ItemCount.ToString();	#2

The SyncChanges method retrieves the Web Part with which it is associated (#1) and then assigns the values from the Web Part to the local controls (#2).

Copying the values from Editor Part to Web Part

The property values are written back to the Web Part when you click OK or Apply in the tool pane. When saved, the
Editor Part invokes a method called ApplyChanges that you need to implement so that it copies the values from the Editor Part to the Web Part. Listing 3 shows you how this method is implemented.

 Listing 3 The ApplyChanges method in the Editor Part that pushes back the property values to the Web Part  

public override bool ApplyChanges() { EnsureChildControls();
RssWebPart webPart = this.WebPartToEdit as RssWebPart;	#1 if (webPart != null) {
webPart.RssFeedUrl = m_feedUrl.Text;	#2 webPart.ItemCount = int.Parse(m_itemCount.Text);	#2
return true;	#3

Just as in the SyncChanges method, you have to retrieve the associated Web Part (#1) and then set the properties of the Web Part to the values of the local controls (#2). Finally, you have to return true (#3) to indicate that the operation was successful. If the ApplyChanges method returns false, then the SyncChanges method will not be invoked, since an error has occurred.
This is all that is needed to create the custom Editor Part.


In this brief article, we discussed the steps to create a custom Editor Part including adding a new class, creating a user interface, populating the controls in Editor Part, and copying the values from the Editor Part to the Web Part.

SharePoint 2010 Web Parts in Action

Wictor Wilén
MEAP Release: March 2010
Softbound print: December 2010 (est.) | 375 pages
ISBN: 9781935182771

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend