SharePoint custom EditorPart like default
Have you ever want to create the EditorPart for WebPart with the behavior : Expand / Collapse like SharePoint default.
The below code will help you to do that, with a note: Every control you create, you need add them to ControlPanel control
All the EditorPart, you should derive from MyEditorPart class
public abstract class MyEditorPart : EditorPart
{
public Panel ControlPanel = new Panel();
Assembly spAssembly = Assembly.Load(@"Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public;
Panel propertyPanel;
public MCEditorPart(string PartID, string Title)
{
ID = PartID;
this.Title = Title;
ControlPanel.Attributes["id"] = this.ClientID;
ControlPanel.Style.Add("padding", "1px");
this.ChromeType = PartChromeType.None;
}
//Create the controls to render in the custom tool part editor
protected override void CreateChildControls()
{
base.CreateChildControls();
propertyPanel = (Panel)spAssembly.CreateInstance( "Microsoft.SharePoint.WebPartPages.TPPanel",
false, bindingFlags, null,
new object[] { Title, ControlPanel, (this.ChromeState == PartChromeState.Minimized) },
null, null);
Controls.Add(propertyPanel);
}
}
Works like a charm! Only had to realise that instead of making a this.controls.add(all my controls) I had to add them to the inherited ControlPanel .
ReplyDeleteThanks man.. Finally my EditorPart looks nice!
It really works for me :))
ReplyDeleteThanks a lot
This is one of those little gems you come across from time to time. I've just implemented this in SP 2013.
ReplyDeleteMany thanks and please keep these little nuggets coming.