Adding Custom Properties to a SharePoint Web Part
NOTE: This post is part 2 of the tutorial entitled Building a SharePoint List Aggregator Web Part with Custom Properties.
All SharePoint web parts have properties that control their behavior and appearance. You can access these properties by clicking on a little triangle on the right side of the web part title and then clicking on Modify Shared Web Part. This will open the Property Pane on the right side of the page. The standard property groups are Appearance, Layout, and Advanced. We will add another property group called Options and add the following properties to it:
- Site Collection URLs – A textbox that will allow users enter site collection URLs to aggregate content from, separated by semicolons
- List Type – A dropdown list that will allow users select one of the following list types to aggregate content from: Announcements, Calendar, or Tasks
- Maximum Number of Items – A textbox that will allow users to limit the result set
- Current User Tasks Only – A checkbox that will control whether only the current user’s tasks will be aggregated
Web part properties can only be of a certain type and must have specific attributes.
Web Part Property Types
A custom property will be displayed automatically in the Property Pane if it’s of type bool, DateTime, enum, int, or string. The table below shows how they will be displayed on the Property Pane:
| Property Type | Displayed as |
|---|---|
| bool | Check box |
| DateTime | Text box |
| enum | Dropdown list |
| int | Text box |
| string | Text box |
Web Part Property Attributes
Attributes customize the behavior of the web part in the Property Pane and determine how the property will be stored. The following table lists displays the attributes we will be using:
| Attribute | Description |
|---|---|
| WebBrowsable | Must be set to True to be visible in the Property Pane |
| Personalizable | Controls the personalization scope of the property |
| SPWebCategoryName | The name of the property group this property will show up under |
| WebDisplayName | The caption for the property |
Adding Custom Properties to the List Aggregator Web Part
Let’s add some variables and custom web part properties to our web part. Add the code below directly above the CreateChildControls() method:
#Region “PROPERTIES”
Private _siteCollectionUrls As String = SPContext.Current.Site.Url
Private _listTypeId As Int32 = 104
Private _itemsToDisplay As Int32 = 100
Private _currentUserOnly As Boolean = False
Private SiteCollections As List(Of String)
”’ <summary>
”’ Holds list types selectable by users, values are corresponding server template ids
”’ </summary>
Public Enum myListType
Announcements = 104
Calendar = 106
Tasks = 107
End Enum
”’ <summary>
”’ Allows user to input Site Collection URLs separated by semicolon
”’ Default value (and if input string is empty) is the current Site Collection URL
”’ </summary>
<WebBrowsable(True)> _
<Personalizable(PersonalizationScope.Shared)> _
<SPWebCategoryName(“Options”)> _
<WebDisplayName(“Enter site collection URLs to include, separated by semicolon:”)> _
Public Property SiteCollectionUrls() As String
Get
Return _siteCollectionUrls
End Get
Set(ByVal value As String)
If Not value = String.Empty Then
_siteCollectionUrls = value
Else
_siteCollectionUrls = SPContext.Current.Site.Url
End If
End Set
End Property
”’ <summary>
”’ Allows user to select one of the list types to aggregate data from
”’ </summary>
<WebBrowsable(True)> _
<Personalizable(PersonalizationScope.Shared)> _
<SPWebCategoryName(“Options”)> _
<WebDisplayName(“Show items from this list type only:”)> _
Public Property ListType() As myListType
Get
Select Case _listTypeId
Case 104
Return myListType.Announcements
Case 106
Return myListType.Calendar
Case 107
Return myListType.Tasks
End Select
End Get
Set(ByVal value As myListType)
Select Case value
Case myListType.Announcements
_listTypeId = myListType.Announcements
Case myListType.Calendar
_listTypeId = myListType.Calendar
Case myListType.Tasks
_listTypeId = myListType.Tasks
End Select
End Set
End Property
”’ <summary>
”’ Allows user to input the maximum number of items to display
”’ Default value is 100
”’ </summary>
<WebBrowsable(True)> _
<Personalizable(PersonalizationScope.Shared)> _
<SPWebCategoryName(“Options”)> _
<WebDisplayName(“Maximum number of items to display:”)> _
Public Property ItemsToDisplay() As String
Get
Return _itemsToDisplay
End Get
Set(ByVal value As String)
If Not value = String.Empty Then
_itemsToDisplay = value
Else
_itemsToDisplay = 100
End If
End Set
End Property
”’ <summary>
”’ Allows user to display tasks for currently logged in user only
”’ </summary>
<WebBrowsable(True)> _
<Personalizable(PersonalizationScope.Shared)> _
<SPWebCategoryName(“Options”)> _
<WebDisplayName(“Show tasks for current user only”)> _
Public Property CurrentUserOnly() As Boolean
Get
Return _currentUserOnly
End Get
Set(ByVal value As Boolean)
_currentUserOnly = value
End Set
End Property
#End Region
Now you need to recompile and redeploy the web part to see your changes.
Redeploying a Web Part
Fortunately, redeploying a web part does not involve nearly as many steps as deploying it for the first time. All you need to do this time is:
- Build your project in Visual Studio (Build > Build ListAggregator)
- Deploy a new version of ListAggregator.dll to the GAC
- Reset IIS by opening the Command Prompt window, typing in the “iisreset” command and hitting the Enter key
- Refresh the page this web part is on
Now, open the page for editing, and open the Property Pane for the List Aggregator web part. You should see the four properties we just added. Notice that our custom properties already have default values that we specified in the code.
Continue to part 3: Adding CAML Queries to a Web Part to Collect Data from SharePoint Lists.

Share
Recent Posts