DropDownList in SharePoint WebPart properties

It has been a while since I developed WebParts for SharePoint but in some cases it’s the best solution for the situation. Of course if you’re building a webpart you want to make it configurable using public properties so that users and administrators can assign values to your webpart and manipulate some of it’s execution. SharePoint will render various controls that will depend of the type of variable you’re assigning to a public property.

It’s not immediately apparent what type of variable should you use to get a drop down for your property. So after a little bit of digging and trying things out I found that you can use an enum to get drop down control rendered in your properties.

But enum is not the most flexible approach. QUite often we need to populate things dynamicaly. After trying anything from list to collection I found the following approach which not only will let you outpul drop down list but also any other complex control or set of controls and let you even run your custom events.

Now, I was trying to use this trick in my UserControl webpart so that I can utilize SmartPart to deploy it … but unfortunately this particular workaround didn’t work – so don’t even waste your time trying.

In any case here we go:

1. Create new webpart inheriting from : Microsoft.SharePoint.WebPartPages.WebPart
2. Create properties in your webpart, in my case reusableContent is a property

string reusableContentInstance;
[
Personalizable(PersonalizationScope.Shared),
WebBrowsable(false),
WebDisplayName("Reusable Content to Display"),
WebDescription("Select reusable content to be displayed in this area")
]
public string ReusableContentProp
{
get { return reusableContentInstance; }
set { reusableContentInstance = value; }
}

You’ll notice that I have WebBrowsable set to false – this is because I will hide it from a user .. if I didn’t hide it – SharePoint would generate line of text is Miscelaneous section of Webpart Props.

3. Override method responsible for rendering ToolPart (webpart property tool pane)

public override ToolPart[] GetToolParts()
 {
 ToolPart[] allToolParts = new ToolPart[3];
 WebPartToolPart standardToolParts = new WebPartToolPart();
 CustomPropertyToolPart customToolParts = new CustomPropertyToolPart();
 allToolParts[0] = new CustomToolPart();
 allToolParts[1] = standardToolParts;
 allToolParts[2] = customToolParts;
 return allToolParts;
 }

In here you’ll see that I have my custom props rendered first, the i render standard props and then Miscelaneous. Since I have nothing (everything is hidden) in Miscelaneous – it won’t show up at all.

4. Create a class that will implement rendering your UI for web controls for tool pane,getAvailableContent() is just my routine to get values from the list and display them as drop down values in properties of my webpart:

public class CustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
{
DropDownList ddlAvailableContent;
Panel toolPartPanel;
///
<summary>/// Creates DDL and populates it with values
///</summary>protected override void CreateChildControls()
{
toolPartPanel = new Panel();
ddlAvailableContent = new DropDownList();
ddlAvailableContent.ID = “ddlSelectReusableContent”;
Dictionary availableContent = new Dictionary();
availableContent = getAvailableContent();
foreach (string key in availableContent.Keys)
ddlAvailableContent.Items.Add(key);
toolPartPanel.Controls.Add(ddlAvailableContent);
Controls.Add(toolPartPanel);
base.CreateChildControls();
}
///
<summary>/// Transfers user’s selection on the DDL to the actual hidden webpart property
///</summary>public override void ApplyChanges()
{
ReusableContent wp = (ReusableContent)this.ParentToolPane.SelectedWebPart;
wp.ReusableContentProp = ddlAvailableContent.SelectedValue;
}
///
<summary>/// Helper function, will access Reusable Content list on portal and
/// get all of it’s contents
///</summary>/// Collection of Reusable Contents
public Dictionary getAvailableContent()
{
SPWeb rootWeb = SPContext.Current.Site.OpenWeb(“/”);
Dictionary availableContent = new Dictionary();
if (rootWeb.ListExists(Constants.ReusableContentList))
{
SPList list = rootWeb.Lists[Constants.ReusableContentList];
foreach (SPListItem contentInstance in list.Items)
{
availableContent.Add(contentInstance.Title, contentInstance[Constants.ReusableContentHTML].ToString());
}
}
return availableContent;
}
}

Leave a Reply

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

*

73,101 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation