Adding Custom Properties to Web Parts (SharePoint)
To add a custom property to a web part, you will need to add some lines to get it working. The items in red will be additional to the basic web part project. Don't forget to add the references to the project as well.
Imports System.Xml.Serialization
Imports System.ComponentModel
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.HtmlControls
Namespace TestPart
<Guid("aae404b2-ff7e-4b0f-9dec-a215cf47d907"), DefaultProperty("Text"), ToolboxData("<{0}:StaffDirectory runat=server></{0}:StaffDirectory>"), XmlRoot(Namespace:="TestPart")> _
Public Class TestPart
Inherits System.Web.UI.WebControls.WebParts.WebPart
<Guid("aae404b2-ff7e-4b0f-9dec-a215cf47d907"), DefaultProperty("Text"), ToolboxData("<{0}:StaffDirectory runat=server></{0}:StaffDirectory>"), XmlRoot(Namespace:="TestPart")> _
Public Class TestPart
Inherits System.Web.UI.WebControls.WebParts.WebPart
'This is all part of the custom property definition
Private Const _defaultText As String = ""
Private _NumNewsItems As String = _defaultText
Private Const _defaultText As String = ""
Private _NumNewsItems As String = _defaultText
<Browsable(True), Category("Miscellaneous"), DefaultValue(_defaultText), WebPartStorage(Storage.Personal), FriendlyName("Text"), Description("Text Property")> _
Public Property NumNewsItems() As Integer
Get
Return _NumNewsItems
End Get
Set(ByVal itemValue As Integer)
_NumNewsItems = itemValue
End Set
End Property
Public Property NumNewsItems() As Integer
Get
Return _NumNewsItems
End Get
Set(ByVal itemValue As Integer)
_NumNewsItems = itemValue
End Set
End Property
'Web Part Title
Private Const _defaultTitle As String = ""
Private _NewsTitle As String
Private Const _defaultTitle As String = ""
Private _NewsTitle As String
<Browsable(True), Category("Miscellaneous"), DefaultValue(_defaultTitle), WebPartStorage(Storage.Personal), FriendlyName("Text"), Description("Text Property")> _
Public Property NewsTitle() As String
Get
Return _NewsTitle
End Get
Set(ByVal itemValue As String)
_NewsTitle = itemValue
End Set
End Property
Public Property NewsTitle() As String
Get
Return _NewsTitle
End Get
Set(ByVal itemValue As String)
_NewsTitle = itemValue
End Set
End Property
These properties are then set in the page like this:
<TestPart:TestPart runat="server" ID="TestPart1" NewsTitle="News and Information" NumNewsItems="5" Description="Latest new and Info" NoNewsItems="3" Title="News & Information" ImportErrorMessage="Cannot import TestPart Web Part." __MarkupType="vsattributemarkup" __WebPartId="{f4083814-edde-4dc4-958e-732c18d85257}" WebPart="true" __designer:IsClosed="false"></TestPart:TestPart>