Writing custom properties for web parts in Sharepoint

For adding custom properties to our web part we need to do the following

1) Create property.

2) Decorate the property with the following attributes

WebBrowsable – To allow your property to be visible within SharePoint.

WebDisplayName– To provide display name to the property.

WebDescription– To provide description for that property.

Personalizable – To define the scope of it i.e either User or Shared through PersonalizationScope enumeration.

Let’s take a simple example wherein we have 2 properties defined, user will enter value for them and finally when the web part is rendered, we would be displaying their sum within the web part.

namespace AdditionWebPart


public class SumWebPart : WebPart{

private int firstVariable;


WebDisplayName(“First Value”),

WebDescription(“Enter value for first variable”),


public int FirstVariable


get { return firstVariable; }

set { firstVariable = value; }


private int secondVariable;


WebDisplayName(“Second Value”),

WebDescription(“Enter value for second variable”),


public int SecondVariable


get { return secondVariable; }

set { secondVariable = value; }


protected override void Render(System.Web.UI.HtmlTextWriter writer){

writer.Write(“The total is “ +this.calcTotal(this.firstVariable,this.secondVariable));


private int calcTotal(int a, int b){

return a + b;


Put the following attribute in your assemblyinfo.cs file

[assembly: AllowPartiallyTrustedCallers]

Strong sign the assembly and install it in GAC.

Open the web.config of your site where you want this webpart

Make a safecontrol entry within the web.config for your webpart.

<SafeControl Assembly=AdditionWebPart, Version=, Culture=neutral, PublicKeyToken=5f7492d3f59e0c4b Namespace=AdditionWebPart TypeName=* Safe=True />

The name of the assembly,it’s version, culture and public key token information can be found by right clicking the assembly within gac and selecting properties.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

5 thoughts on “Writing custom properties for web parts in Sharepoint”

  1. Nice, you just solve me half of the problem, thanks

    Now I would like this new custom property to be a dropdown menu (choice)

    How can this be achieved?



  2. Hi Mario,
    For showing the drop down you can define your property as enum.
    public enum myChoiceEnum

    private myChoiceEnum _myEnum;

    WebDisplayName(“My Choice”),
    WebDescription(“Select a value”),
    public myChoiceEnum MyEnum
    get { return _myEnum; }
    set { _myEnum = value; }

    Try it!!


Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.