How to: add properties to custom web parts

You can easily add custom properties to your custom web parts so that you can pass in values to them from the Edit panel. Depending on the nature of the property that you need to add, they differ slightly. The following hold:

 

i. How to add a text box and check box in the properties of a custom web part


private string _columnDisplayName = null;
private bool _doDisplayChildren = false;

[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[System.ComponentModel.Category(“Setup”)]
[WebDisplayName(“Display children of the above “)]
[WebDescription(“The display name of the content type where the following column is being used.”)]
public bool DoDisplayChildren
{
get
{
return _doDisplayChildren;
}
set { _doDisplayChildren = value; }
}
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[System.ComponentModel.Category(“Setup”)]
[WebDisplayName(“Column to aggregate”)]
[WebDescription(“The display name of the ‘Publication content type’ column to aggregate.”)]
public string ColumnDisplayName
{
get
{
if (_columnDisplayName == null || _columnDisplayName == string.Empty)
{
_columnDisplayName = string.Empty;
}
return _columnDisplayName;
}
set { _columnDisplayName = value; }
}

ii. How to add a drop down list in the properties of a custom web part


public enum MnM
{
red,
yellow
}
private MnM _mnm=MnM.red;

[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[System.ComponentModel.Category(“Setup”)]
[WebDisplayName(“Column to aggregate”)]
[WebDescription(“The display name of the ‘Publication content type’ column to aggregate.”)]
public MnM ColumnDisplayName
{
get
{
return _mnm;
}
set
{
_mnm = value;
}
}

Explanation:


1. The enum at the top will be used to display the values you want in your drop down menu in the properties of the web part (MnM)
2. you will need a private field/variable of the type you just created ( the enum above ) (_mnm)
3. you will need to define personalizable and webbrowsable properties for your custom web part properties
4. the System.ComponentModel.Category gives a title to the section of the web part properties where your new properties will appear
5. the webdescription is just information for the user filling in the web part properties
6. the mechanics of the properties and how the values are stored and read back are in the following bit of code.
a. You wil need a public property of the type you created (the enum – just like you did with the private one in the previous steps)
b. the get will read the saved value so that it will be displayed back to the browser when the user accesses the web part properties again (return gives the value stored in the private field)
c. the set will save the selected value from the drop down in the web part properties to the private field you created (value is the value selected by the user setting up the web part)

Real life example:


public enum BrowsableColumn //the value of the drop down to be displayed in the web part properties
{
Publisher,
Subthemes,
Themes,
Publication_Year
}
private BrowsableColumn _BrowsableColumn = BrowsableColumn.Themes; //instantiate and give initial value to private object of type of the enum above (where the selected values will be stored)

//define the web part properties
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[System.ComponentModel.Category(“Setup”)]
[WebDisplayName(“Column to aggregate”)]
[WebDescription(“The display name of the ‘Publication content type’ column to aggregate.”)]
public BrowsableColumn ColumnDisplayName
{
get
{
return _BrowsableColumn; //the following should work properly but we removed them | we were getting a: Cannot save the property settings for this web part. Exception occured ( Exception from HRESULT – 0x80020009 (DISP_E_EXCEPTION)
}
set
{
_BrowsableColumn = value;
}
}

#160;

MGR: the Intelogist

About MGR: the Intelogist

SharePoint Server developer, turned Sitefinity developer, turned Angular developer, turned SharePoint Online consultant, turned Unily consultant, turned O365 consultant... Never a dull moment!

Leave a Reply

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

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=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.