Creating Custom Editor Part for Sharepoint Webpart
Referred from URL:
http://sharepointconsultant.blogspot.com/2009/04/creating-custom-editor-part-for.html
What is Editor part and why do we need them?
Well for this question to answer, basically all webparts will have a property pane when we select modify this webpart. This tool/property pane allows to set properties to webparts.
Now what if we need to customize this property pane? Say client needs to have a set of controls where user can select values from these controls which inturn feeds to webpart, so that webpart takes these properties changes.
Let me give you an example,
I am writing a editor part which has couple of dropdownlists. These drop down list will get infomration from differetn field values from from list/document library. Now user can select the required values/fields in the dropdownlist, and webpart will take these values.
So without wasting any moments,
lets get the ground running,
1. Create a webpart in a normal mode with all web browasable properties and get this up & running.
2. Once it is ok, now to appreciate the purpose of editor part,
3. create a class inherting EditorPart. You can see this class as a normal webpart. Here you need to create controls to be rendered by using same old createchildcontrols, RenderContents and so on.
4. Now there are two methods which you need to take a bit care.
(*) Applychanges
(*) SyncChanges
Applychanges is the method which takes all the information from the editor part controls/properties and passes it to WebPart in question's Property.
SyncChanges - as name signifies, it synchronies the values between Editor Part and Webpart.
Now Lastly, in the webpart class, override EditorPart method basically creates a object of your custom editor part and sends it to webpart manager/framework. Basically this method tells that when ever a users click on modify this webpart, then instead of displaying a normal property panel create a custom property panel with this object. One more caveat is that, make all webbrowsable property false in the webpart.
Below is the code for EditorPart,
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace kiran.webparts
{
class MyEditorPart :EditorPart
{
private TextBox txtbx;
private Button btnClick;
private CustomControls.SearchControl srcCntrl;
protected override void CreateChildControls()
{
// base.CreateChildControls();
this.Controls.Add(txtbx);
//this.Controls.Add(btnClick);
this.Controls.Add(srcCntrl);
}
public MyEditorPart()
{
this.ID = "MyEditorpart";
txtbx = new TextBox();
btnClick = new Button();
srcCntrl = new CustomControls.SearchControl();
btnClick.Click += new EventHandler(btnClick_Click);
}
void btnClick_Click(object sender, EventArgs e)
{
}
public override bool ApplyChanges()
{
((WebpartWithEditorPart)WebPartToEdit).Phone = txtbx.Text;
return true;
}
public override void SyncChanges()
{
txtbx.Text = ((WebpartWithEditorPart)WebPartToEdit).Phone;
}
protected override void Render(HtmlTextWriter writer)
{
txtbx.RenderControl(writer);
btnClick.RenderControl(writer);
srcCntrl.RenderControl(writer);
}
protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
}
}
}
Below is the code for Webpart in question.
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;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace kiran.webparts
{
[Guid("71c2ac74-6ffa-4159-b6ed-7807335fd093")]
public class WebpartWithEditorPart : System.Web.UI.WebControls.WebParts.WebPart
{
public WebpartWithEditorPart()
{
}
TextBox txtbx;
Button btn;
string m_phone;
[Personalizable(PersonalizationScope.Shared), WebBrowsable(false),WebDisplayName("Phone"),WebDescription("Phone number")]
public string Phone
{
get { return m_phone; }
set { m_phone = value; }
}
protected override void CreateChildControls()
{
base.CreateChildControls();
txtbx = new TextBox();
btn = new Button();
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(txtbx);
this.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
txtbx.Text = "Value from the property is " + Phone;
}
public override EditorPartCollection CreateEditorParts()
{
//return base.CreateEditorParts();
ArrayList alist = new ArrayList();
MyEditorPart myeditorpart = new MyEditorPart();
myeditorpart.ID = this.ID + "_EditorPart";
myeditorpart.Title = "Phone Number";
myeditorpart.GroupingText = "";
alist.Add(myeditorpart);
EditorPartCollection EdPartColl = new EditorPartCollection(alist);
return EdPartColl;
}
}
}
0 comments