Creating Visual Web Part Properties

By | April 27, 2011

Before reading this article I assume you already know how to create your own web parts in Sharepoint and is looking for some ways to create a property for that webpart that you can easily Set and Get from your application like this one on the image below.


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 Web Part Class (yellow highlight) and not the User Control Class (green highlight) define your properties

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

[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 noticed each property you created have attributes and here is what it means

  • Category – This will group your property according to category, If not declared “Miscellaneous” will be used as a default.
  • Personalizable – How the web part is configured which can be per-user (PersonalizationScope.User) basis or for all 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 an illegal values

So the final step is conusming that property in your WebPart, you can do it easily by doing like such.


8 thoughts on “Creating Visual Web Part Properties

  1. Steve Heyes

    Hi, thanks for the article. I had an issue accessing the property from the user control. Issues occured when there is more than one instance of the web part on the site. The solution was,

    var parent = (Sample_Web_Part ) Parent;
    Label1.Text = parent.SampleText;

    Reply
  2. Ben

    Thanks for this code. By the way, you need to remove ‘static’ from the variable declarations. If you have static (eg public static int SampleNumber) you will loose the property value after every IIS restart.

    Once you remove static, the data is persisted and stored by SharePoint.

    Reply
  3. Pingback: Creating Visual Web Part Custom Properties | Ask Noor

Leave a Reply