Create rich web part properties controls using custom tool parts with a complete example code

Standard

The traditional way to add a custom properties to your custom web part is creating a normal asp.net accessors properties  with attrbuites that’s used to specify this property is a web part property or not , these attributes like  Browsable , Category , DefaultValue , FriendlyNameAttribute , etc… , those  traditional properties displayed based on their types , there are  fixed types only for rendering controls for those properties see the table below

Property Type Displayed in property pane as
bool Check box
DateTime Text box
enum Dropdown
int Text box
string Text box

Suppose you want to add radio buttons groups or calendar control or any control sure you can’t do this with this way but you can add your dreamily controls with events as you need With Tool Part class see the image below to get the motivation to know ! 🙂

Steps to work

create a custom Web Part (from Microsoft.SharePoint.WebPartPages.WebPart) then override the method “GetToolParts()” the base method get all tool parts in this web part and by overriding this method we can get add our custom tool part to the tool parts collection see this code


public override ToolPart[] GetToolParts()
{

List<ToolPart> toolParts = null;

toolParts = new List<ToolPart>(base.GetToolParts());

toolParts.Insert(0, new TestToolPart());

return toolParts.ToArray();

}

The base method return a list of toolparts class , we will use the insert method to insert our custom tool part (TestToolPart is our custom toolpart it’s a class that inherit from Microsoft.SharePoint.WebPartPages.ToolPart)in this collection , index in insert method will order the displaying of tool parts , here we put a 0 that’s means this tool part will be the first one

Notes: you will need in any case to pass some parameters from the web part to the tool part itself you can manage this easy by creating public properties in the tool part and assign it for example assigning the title of the tool part like this

TestToolPart testTP=new TestToolPart(_siteURL);

testTP.Title="All Lists";

toolParts = new List<ToolPart>(base.GetToolParts());

toolParts.Insert(0, testTP);

or you can create another constructor for the the custom toolpart class  with some parameters and pass it also before adding to the toolparts collection

Custom ToolPart Class

In this custom class for our example I created a private property for the tool part from the type RadioButtonList

private RadioButtonList _rdListTitles=new RadioButtonList();

I created the radio buttons group as a property in this class to add the data source and bind  radio buttons in the CreateChildControls() method then get the selected value in another method (ApplyChanges() method)  we will describe  both here and sure you have the ability to do another patterns for this

CreateChildControls () Method

I will override the CreateChildControls() method protected override void CreateChildControls()

this method for adding your controls that will display in this tool part see the code below for our example

protected override void CreateChildControls()

{

base.CreateChildControls();

_rdListTitles.DataSource = {ANY DATA SOURCE}

_rdListTitles.DataTextField = “Title”;

_rdListTitles.DataValueField = “ID”;

_rdListTitles.DataBind();

this.Controls.Add(_rdListTitles);

}

We created the radio button list and assign a data source for it here , replace {ANY DATA SOURCE}

your data source method

ApplyChanges () Method

public override void ApplyChanges();  this method will execute when the user click on “ok” or “apply” button on the property pane for the web part so you can pass the values here from your control to the web part , for example we have some radio buttons with labels all lists for the site , when the user choose the list the web part will get the choice of the user see the code below
public override void ApplyChanges()
{

base.ApplyChanges();

TestWebPartToolPart parentWebPart = (TestWebPartToolPart)this.ParentToolPane.SelectedWebPart;

if (!string.IsNullOrEmpty(_rdListTitles.SelectedItem.Value))

parentWebPart.ListID = _rdListTitles.SelectedItem.Value;

}

As we see we get a reference to our custom web part “TestWebPartToolPart”  by getting the web part for the parent tool pane that contain our tool part by this.ParentToolPane.SelectedWebPart this will return Microsoft.SharePoint.WebPartPages.WebPart so we cast it to our web part TestWebPartToolPart parentWebPart = (TestWebPartToolPart)this.ParentToolPane.SelectedWebPart then fill a public property for the web part (ListID) with the selected item value from the radio button.

Now have a look for this complete example code ,our example to  print some information about the selected list from the custom tool part that we did

Custom Web Part Class example code


namespace TestToolPartsNS
{
[Guid("4b3c4d66-afaa-4256-a1b8-05ca5f6b95d2")]
public class TestWebPartToolPart : Microsoft.SharePoint.WebPartPages.WebPart
{

private string _siteURL = “YOUR SITE URL”;

private string _listID;

public string ListID

{

set

{

_listID = value;

}

get

{

return _listID;

}

}

protected override void CreateChildControls()

{

base.CreateChildControls();

if (!string.IsNullOrEmpty(this._listID))

{

Label lbl = new Label();

DetailsView dView = new DetailsView();

SPSecurity.RunWithElevatedPrivileges(delegate()

{

using (SPSite site = new SPSite(_siteURL))

{

using (SPWeb spweb = site.OpenWeb())

{

SPList list = spweb.Lists[new Guid(this._listID)];

lbl.Text =    string.Format(“<h3>{0} List Information</h3>”, list.Title);

lbl.Text += string.Format(“<strong>List Items Count:</strong> {0}.<br />”, list.Items.Count.ToString());

lbl.Text += string.Format(“<strong>List Default View:</strong> {0}.<br />”, list.DefaultView.Title);

lbl.Text += string.Format(“<strong>List Guid:</strong> {0}.<br />”, list.ID.ToString());

lbl.Text += string.Format(“<strong>List Created Date:</strong> {0}.<br />”, list.Created.ToString());

}

}

});

this.Controls.Add(lbl);

}

}

public override ToolPart[] GetToolParts()

{

List<ToolPart> toolParts = null;

SPSecurity.RunWithElevatedPrivileges(delegate()

{

using (SPSite site = new SPSite(_siteURL))

{

using (SPWeb spweb = site.OpenWeb())

{

TestToolPart testTP=new TestToolPart(_siteURL);

testTP.Title=”All Lists”;

toolParts = new List<ToolPart>(base.GetToolParts());

toolParts.Insert(0, testTP);

}

}

});

return toolParts.ToArray();

}

}

}

ToolPart class example code


namespace TestToolPartsNS

{

class TestToolPart : Microsoft.SharePoint.WebPartPages.ToolPart

{

private string _webUrl;

private RadioButtonList _rdListTitles=new RadioButtonList();

public TestToolPart(string pWebUrl)

{

this._webUrl = pWebUrl;

}

protected override void CreateChildControls()

{

base.CreateChildControls();

_rdListTitles.DataSource = getAllListTitles();

_rdListTitles.DataTextField = “Title”;

_rdListTitles.DataValueField = “ID”;

_rdListTitles.DataBind();

this.Controls.Add(_rdListTitles);

}

public override void ApplyChanges()

{

base.ApplyChanges();

TestWebPartToolPart parentWebPart = (TestWebPartToolPart)this.ParentToolPane.SelectedWebPart;

if (!string.IsNullOrEmpty(_rdListTitles.SelectedItem.Value))

parentWebPart.ListID = _rdListTitles.SelectedItem.Value;

}

private DataTable getAllListTitles()

{

DataTable dtListTitles = new DataTable();

dtListTitles.Columns.Add(new DataColumn(“ID”));

dtListTitles.Columns.Add(new DataColumn(“Title”));

DataRow dataRow = null;

SPSecurity.RunWithElevatedPrivileges(delegate()

{

using (SPSite site = new SPSite(_webUrl))

{

using (SPWeb spweb = site.OpenWeb())

{

foreach (SPList list in spweb.Lists)

{

dataRow = dtListTitles.NewRow();

dataRow[“ID”] = list.ID;

dataRow[“Title”] = list.Title;

dtListTitles.Rows.Add(dataRow);

}

}

}

});

if (dtListTitles.Rows.Count > 0)

return dtListTitles;

return null;

}

}

}

Hope it will help.
Thanks

Advertisements

2 thoughts on “Create rich web part properties controls using custom tool parts with a complete example code

    • Create a button object in ToolPart class then add it in this ToolPart like this

      Button btn=new Button();

      This.Controls.Add(btn);

      check the ToolPart class example in this article

      Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s