Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / C#

Creating Visual Web Part Properties

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
15 May 2011CPOL1 min read 75.7K   7   11
How to create WebPart properties.

Before reading this article, I assume you already know how to create your own WebParts in SharePoint and is looking for some way to create a property for that WebPart that you can easily Set and Get from your application, like the one on the image below:

Image 1

but if not, please read this post regarding Developing Web Parts first to avoid confusion. But if you already know what to do, then continue on.

First, on your WebPart class (yellow highlight), and not the User Control class (green highlight), define your properties:

Image 2

For this example, I will show you how to define a text, boolean, integer, date/time, and enumeration property:

C#
[ToolboxItemAttribute(false)]
public class Sample_Web_Part : WebPart
{
    // Visual Studio might automatically update
    // this path when you change the Visual Web Part project item.
    private const string _ascxPath = 
        @"~/_CONTROLTEMPLATES/Sample_Project/" + 
        @"Sample Web Part/Sample Web PartUserControl.ascx";

    protected override void CreateChildControls()
    {
        Control control = Page.LoadControl(_ascxPath);
        Controls.Add(control);
    }

    public static Boolean SampleBoolean;
    [Category("Extended Settings"),
    Personalizable(PersonalizationScope.Shared),
    WebBrowsable(true),
    WebDisplayName("Sample Boolean"),
    WebDescription("Please Choose a Sample Boolean")]
    public Boolean _SampleBoolean
    {
        get { return SampleBoolean; }
        set { SampleBoolean = value; }
    }

    public static string SampleText;
    [Category("Extended Settings"),
    Personalizable(PersonalizationScope.Shared),
    WebBrowsable(true),
    WebDisplayName("Sample Text"),
    WebDescription("Please Enter a Sample Text")]
    public string _SampleText
    {
        get { return SampleText; }
        set
        {
            // Sample Validation
            Regex oRegEx = new Regex("[a-zA-Z]+");
            if (!oRegEx.IsMatch(value))
                throw new Microsoft.SharePoint.WebPartPages.
                    WebPartPageUserException(
                    "Please enter alphabeth characters only");
            SampleText = value;
        }
    }

    public static int SampleNumber;
    [Category("Extended Settings"),
    Personalizable(PersonalizationScope.Shared),
    WebBrowsable(true),
    WebDisplayName("Sample Number"),
    WebDescription("Please Enter a Sample Number")]
    public int _SampleNumber
    {
        get { return SampleNumber; }
        set { SampleNumber = value; }
    }

    public static DateTime SampleDate;
    [Category("Extended Settings"),
    Personalizable(PersonalizationScope.Shared),
    WebBrowsable(true),
    WebDisplayName("Sample Date"),
    WebDescription("Please Enter a Sample Date")]
    public DateTime _SampleDate
    {
        get { return SampleDate; }
        set { SampleDate = value; }
    }

    public enum CityEnum { Manila, Berlin, Auckland, Zurich };
    public static CityEnum SampleDropDown;
    [Category("Extended Settings"),
    Personalizable(PersonalizationScope.Shared),
    WebBrowsable(true),
    WebDisplayName("Sample Drop Down"),
    WebDescription("Please Choose a Sample DropDown")]
    public CityEnum _SampleDropDown
    {
        get { return SampleDropDown; }
        set { SampleDropDown = value; }
    }
}

If you notice, each property you create has attributes:

  • Category – This will group your property according to category. If not declared, “Miscellaneous” will be used as the default.
  • Personalizable – How the WebPart is configured, which can be per-user (PersonalizationScope.User), or for everyone (PersonalizationScope.Shared). For this example, we have chosen all users.
  • WebBrowsable – This will hide or show the property on the tool pane.
  • WebDisplayName – Label for the property.
  • WebDescription – Description for the property.

You might also notice that we have validation on the codes. You can add your own like the one on the SampleText property where we implemented a Regular Expression, or even in SampleNumber. It is by default validated by its type so you cannot save once it has illegal values.

Image 3

So the final step is consuming that property in your WebPart; you can do it easily like:.

Image 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

Comments and Discussions

 
Questionhow to make dropdown property page specific? Pin
Member 99266851-May-18 2:44
Member 99266851-May-18 2:44 
Answerthis is the correct way to set custom property in Visual web Part Pin
Fabio Barbieri24-Apr-15 1:03
Fabio Barbieri24-Apr-15 1:03 
QuestionCaching kind of issue Pin
DIPAYAN DASGUPTA25-Feb-15 18:36
DIPAYAN DASGUPTA25-Feb-15 18:36 
AnswerRe: Caching kind of issue Pin
lukeo_au11-Mar-17 22:45
lukeo_au11-Mar-17 22:45 
QuestionA question Pin
@Ric10-Nov-14 16:11
@Ric10-Nov-14 16:11 
AnswerRe: A question Pin
@Ric10-Nov-14 16:57
@Ric10-Nov-14 16:57 
QuestionThanks Pin
SajithG23-May-14 1:21
SajithG23-May-14 1:21 
QuestionGreat article! Pin
neoNewf26-Mar-14 6:25
neoNewf26-Mar-14 6:25 
AnswerRe: Great article! Pin
@Ric10-Nov-14 16:20
@Ric10-Nov-14 16:20 
GeneralRe: Great article! Pin
@Ric10-Nov-14 16:59
@Ric10-Nov-14 16:59 
GeneralMy vote of 5 Pin
Pola A. Edward5-Dec-12 1:42
Pola A. Edward5-Dec-12 1:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.