Tuesday, November 17, 2009

Webpart custom properties

Scenario:
I use lot of configurable properties in my Webpart.

Always end in spending 5-10 min to make it work because of stupid Microsoft article ( Creating a Web Part with Custom Properties ) , which always shows up as number 1 result when you google, which talk about WebPart inherited from Microsoft.SharePoint.WebPartPages.WebPart class

Solution:
Here's the 100% right way to do it.

Code:

using System.ComponentModel;

using System.Web.UI.WebControls.WebParts;

namespace MyClient
{
public class NewWP : System.Web.UI.WebControls.WebParts.WebPart
{

private const string const_configuationListName = "xConfigurationMasterList";
private string m_configuationListName = const_configuationListName;

// Configuration List Name property.
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[Category("Custom Properties")]
[WebDisplayName("Configuration List Name")]
[Description("Configuration List Name")]
[DefaultValue(const_configuationListName)]
public string ConfigurationListName
{
get
{
return m_configuationListName;
}

set
{
m_configuationListName = value;
}
}

...

}
}

2 comments:

Unknown November 17, 2009 at 2:35 PM  

Hi Sandeep,

Very nice article.

I need something more.

On page load itself i would like to fill the textbox dynamically from the sharepoint list data.

How can i achieve this one...

Frithjof S.,  November 27, 2009 at 8:07 AM  

Hey man,
Thanks for your "how to"... I was unable to get the properties displayed on the edit mode of my webpart.
I followed the example published by microsoft as well and it wouldn't work.

I went crazy because I did everything the ms' tutorial told me to...

Thanks alot! My weekend has been saved :-)