How can we create a custom editor for WebPart?

 Posted by ArticlesMaint on 9/24/2009 | Category: SharePoint Interview questions | Views: 4585

There are situations where you want to use your own custom WebPart editor rather than using the default WebPart editor.

Note: - In case you do not know how to deploy a WebPart we advice you to read the answer for question “Can you explain the 6 steps we need to create a WebPart in SharePoint?

To make your own custom web part editor is a four step procedure.

Step 1: - So the first the thing is to create the WebPart. We need to make a slight change i.e. ‘WebBrowsable (false)” so that it does not use the default ‘WebPart’ editor. Below is the code snippet of the same font size ‘WebPart’ which has ‘WebBrowsable’ value as false.



Step 2 : - The other thing which we need to do in the WebPart is override the ‘CreateEditorParts’ method and add our custom webpart editor. We will be seeing in the next step how we can create the web part editor. For this example we have created ‘SimpleWebPartEditor’.



Step 3: - In order to create your custom web part editor the first step we need to do is inherit from ‘EditorPart’ class. Currently we have created a custom web part editor class called as ‘SimpleLabelWebPart’. In this custom class we have create two labels and two text boxes. The two textboxes will take font size and label value while the labels will display description for those text boxes. In other words we have defined our UI which take inputs which can customize our web part.



Step 4: - Two activities takes place in general one is when we the user gives customization values which customizes the web part and the other when we need to synchronize the customization values with web part.



The web part customization values are stored in content database according to user logged in.  So when customization data is provided for a web part through our custom web part editor we need to override the ‘ApplyChanges’ method and push the customization data to the web part object. So in other words the data is pushed from the web part editor to the content database on per user basis.


Now comes the second aspect where we need to synch the web part data from content database with the web part itself. For this we need to override the ‘syncchanges’ method and push the data from the web part object in to the web part editor user interface.

Ok , now if you edit your web part you can see how the custom editor looks like.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response