Create Custom WebPart Property for SharePoint Visual WebPart

In this hint, I will explain How to create a custom WebPart property within Visual WebPart properties via Visual Studio in SharePoint 2010 /2013.

Scenario:

I have been created a new Visual Web Part “QuickNews” within SharePoint 2013 via Visual Studio Community 2013 to show all related news based on a project ID field as a property in Web Part properties as shown below.

Web Part Settings - custom property

Steps :

  • Open Visual Studio.
  • From Solution Explorer > Double click on .asx.cs file.

Visual Studio solution explorer

  • Add the following code below class definition.
[WebBrowsable(true),
WebDisplayName("Project ID"),
WebDescription("Enter Project Number"),
Personalizable(PersonalizationScope.Shared),
Category("Project Settings")]
public string ProjectID { get; set; }

custom property visual web part source code

  • Where :
    • WebBrowsable attribute enables the property to be displayed in edit mode of Web Part. If set to false, this property won’t be available for the users to change the property by editing the Web Part.
    • WebDisplayName attribute is the user-friendly display name for this property.
    • WebDescription attribute acts as a tool tip for WebDisplayName attribute.
    • Personalizable attribute accepts PersonalizationScope enumerator as an input. This enumerator has two options one is Shared, and another one is User. If this attribute is set as Shared, the changes to this property will be available to all users. If this attribute is set to User, then changed property value will be available only for that user alone.
    • Category attribute is used to display this property under a specified group.
  • Deploy the SharePoint solution.
  • Go to your page to add Web Part > From right side click on arrow > Select Edit Web Part.

edit sharepoint page webpart

  • The new “Project ID” property should be now added successfully.

Web Part Settings - custom property

Finally, To get the property value in code,  you should use the “ProjectID” variable that defined as

public string ProjectID { get; set; }

Enjoy 🙂

Advertisements

5 thoughts on “Create Custom WebPart Property for SharePoint Visual WebPart

  1. HI Qassas,
    I need to access the property in the UserControl Page( UserControl.ascx.cs). can you please share the sample code

    Like

Leave a Reply

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s