EditorPart does not have an ID

Yet another error that I’ve encountered when customizing ToolPart (aka EditorPart) for my own SharePoint Web Part.

The task is to create a custom tool part with custom control that displays a list of SharePoint Lists. This custom control contains a post back logic to update another drop down list base on the selected value.

In order to display your toolpart into the Web Part properties pane, you would need to override the following virtual method in System.Web.UI.WebControls.WebParts.WebPart class

[sourcecode language=”c-sharp”]

public override EditorPartCollection CreateEditorParts()

[/sourcecode]

I bumped into this error “EditorPart does not have an ID” with the following code that was not done correctly

[sourcecode language=”c-sharp”]

public override EditorPartCollection CreateEditorParts()
{
ArrayList editorArray = new ArrayList();
MyEditorPart part = new MyEditorPart() { ID = this.ID + "_editorPart1" };
editorArray.Add(part);
editorArray.Add(new WebPartToolPart() );
editorArray.Add(new CustomPropertyToolPart());
EditorPartCollection editorParts = new EditorPartCollection(editorArray);
return editorParts;

}

[/sourcecode]

The use of including WebPartToolPart and CustompropertyToolPart is to ensure that your custom web part would still display the OOTB Web Part Properties pane (which has the functionality to change Height, Width, ChromeType etc etc)

To use them correctly, you would need to specify an ID for each of the class. After applying the code below, the error is gone.

[sourcecode language=”c-sharp”]

public override EditorPartCollection CreateEditorParts()
{
ArrayList editorArray = new ArrayList();
MyEditorPart part = new MyEditorPart() { ID = this.ID + "_editorPart" };
editorArray.Add(part);
WebPartToolPart wpToolPart = new WebPartToolPart() { ID = this.ID + "_wpToolPart" };
editorArray.Add(wpToolPart);

CustomPropertyToolPart cpToolPart = new CustomPropertyToolPart() { ID = this.ID + "_cpToolPart" };
editorArray.Add(cpToolPart);
EditorPartCollection editorParts = new EditorPartCollection(editorArray);
return editorParts;
}

[/sourcecode]

Leave a Reply

Your email address will not be published. Required fields are marked *