Active Directory Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail
How Long For Me
<< Back To All Blogs
Creating a dynamic SharePoint settings DropDown using a ToolPart
Tuesday, April 21st, 2009
In order to create a dropdown in the settings view of SharePoint it is required that you use an enum with the out-of-box behavior. Each enum item will be presented as an item in the dropdown.
What happens if you want to have a dropdown that is generated dynamically each time the settings window is opened? Enter the world of custom ToolParts.
A toolpart is a sub-section of the settings window when you go to edit the settings of a webpart. Unfortunately they are not easily customizable to flow in with your other settings, so I actually ended up moving all of my settings to the custom toolpart, just for consistency.
Using a custom toolpart requires that you do two things:
1. Create a class that overrides Microsoft.SharePoint.WebPartPages.ToolPart
2. Override the GetToolParts section of your WebPart
I will step through each of these.
In order to create your custom toolpart, you must create a class that overrides Microsoft.SharePoint.WebPartPages.ToolPart. My example code is posted below with comments inline:
public class MyToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
// This is the actual dropdown control
System.Web.UI.WebControls.DropDownList m_Dropdown = new System.Web.UI.WebControls.DropDownList();
// Reference to the parent web part
MyWebPart m_Parent = null;
// Set the title of our toolpart
this.Title = "Settings";
protected override void CreateChildControls()
// Get the instance of the web part
m_Parent = (MyWebPart)ParentToolPane.SelectedWebPart;
// Add some items to the dropdown, this is also where you would dynamically populate any items to add
// Add the dropdown to the actual toolpart controls
public override void ApplyChanges()
// Set the value on apply. This is using a public property on the webpart
m_Parent.MyValue = m_Dropdown.SelectedValue;
Now we need to manipulate the web part class so that it works with the toolpart:
public class MyWebPart : Microsoft.SharePoint.WebPartPages.WebPart
private string m_MyValue = string.Empty;
// This is what we use to set the value from the toolpart's ApplyChanges() method
public string MyValue
m_MyValue = value;
protected override void Render(HtmlTextWriter writer)
// Normal web part code would be here. You could then use the set value from the toolpart in your code as you normally would with a property from a web part
// This is the overwritten method to get the custom toolpart
public override ToolPart GetToolParts()
// Create an array of toolparts to return
ToolPart toReturn = new ToolPart;
// Normal webpart properties like size, width, title, etc
WebPartToolPart wptp = new WebPartToolPart();
// Any properties that would come from your webpart
CustomPropertyToolPart cptp = new CustomPropertyToolPart();
// Add your custom toolpart
MyToolPart mtp = new MyToolPart();
toReturn = wptp;
toReturn = cptp;
toReturn = mtp;
Compile this as you normally would for a web part, deploy, and you're good to go.
A note about the sequence of layout in these toolparts: They appear in the direction in which they were added. So in the case of my array above, the custom toolpart would appear last in the settings zone.
ToolPartin' Tom Out.
Fixing SharePoint error Log with "Preserving template record"
Adding jQuery to SharePoint
Starting SharePoint workflow for each item in a list
Retrieving data from SharePoint SOAP Requests using LINQ
Fixing "Error in loading DLL" in SharePoint
Tom said on Tuesday, September 15th, 2009 @ 6:29 AM
Thanks for the catches, these are issues I came across while copying the code. Thanks for the feedback!
Gordon Hickley said on Monday, September 14th, 2009 @ 8:49 PM
Thanks for this example. A see a couple of issues with the code:
MyWebPart view m_Parent = null; - this doesn't look quite right
DynamicGroupDropdown() - this never gets called
Add A Comment
Email Address: (not public, used to send notifications on further comments)
Enter the text above, except for the 1st and last character: