Adding Properties
Any property you add to your web part class is a web part property. The trick is getting those properties to interact with SharePoint. The most common tasks you need to perform with web part properties are:
Store them between visits (SharePoint calls this personalization).
Display them in the property pane (make them browsable).
Get and set their values from web part controls.
To store a property:
Add a
Personalization
attribute to the property definition.
Properties can be stored for individual users or shared among all users. This is referred to as personalization scope (user or shared). Properties can also be marked as sensitive to prevent them from being exported.
To display a property in the web part property pane:
Add
WebBrowsable
,WebDisplayName
,WebDescription
, andCategory
attributes to the property definition.
Those attributes control the appearance of the property in the web part property pane. For example, the following set of attributes creates a property that is displayed as shown in Figure 11-11. The property has a shared scope, meaning changes are seen by all users.
Figure 11-11. Displaying the custom Text property
// Text property definition private const string defaultText = "default text"; private string _text = defaultText; [Personalizable(PersonalizationScope.Shared, false)] // Storage. [Category("Custom")] // Section in properties pane. [WebBrowsable(true)] ...
Get Essential SharePoint 2007, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.