Creating Custom WebPart Property - SharePoint

This blog will discuss how to create a custom WebPart property in SharePoint 2013.

What is a WebPart?

WebPart in SharePoint, is simillar to a user control. The main difference is, user control cannot be shared across multiple web applications. So if you think, we can user custom controls, we cannot drag and drop them unlike user control. Web Parts essentially fills this gap and combines the above featues. That means you can drag and drop Web Parts at the same time it can be deployed at Site Collection level and be reused across all the sites under it.

Syntax for declaring custom property is similalr to the way you create a property for a standard ASP.NET Web Form control:

You can remember the two parts involved in the declaration,
  • Creating the property accessors.
  • Setting Web Part property attributes.

Create a custom Web Part property

Each custom property for your Web Part requires the appropriate get and set accessor methods. In C#, a typical property for a Web Part takes this form,

  1. private propertyTypeVariable[  
  2.         [Attribute1, Attribute2, ...] public PropertyTypeProperty {  
  3.             get {  
  4.                 return propertyTypeVariable;  
  5.             }  
  6.             set {  
  7.                 propertyTypeVariable = value;  
  8.             }  
  9.         }  

 

The attributes you declare for your custom property determine how it is stored and displayed to users when they personalize your Web Part.

It will be easy to understand when we do a hand on. So, let us create a SharePoint Project and add a WebPart to it.

I am using Visual Studio 2013 for demo here.

Open Visual Studio

File -> New -> Project

Choose SharePoint 2013 – Empty Project from Office/SharePoint sub menu under Visual C#.

Name the project as WebpartPropertySample.

Click OK.

In the next screen, choose the “Farm Solution” radio button and click OK.

Now we need to add a visual WebPart to the project.

Create a new folder in the project and name it Webparts.

Right click on the Webparts folder and Add -> New Item -> Office/SharePoint

Choose Visual Web Part (Farm Solution Only) from the list of items.

Name it CustomPropertyWebpart and click Add.

Double click on the CustomPropertyWebpartUserControl.Aspx.cs file and declare an object of the CustomPropertyWebpart.

public CustomPropertyWebpart customPropertyWebPart;

Now, open CustomPropertyWebpart.cs file.

Let us create a custom web part property with name CustomPropertyName.

You can specify where in the Web part properties section, your custom property should appear.

I choose the custom property to appear under the new custom section Sample Custom Properties.

I have added some code to show the user that the web part is in edit mode while it is being edited by user. This is done inside CreateChildControls method. 

  1. using System;  
  2. using System.ComponentModel;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls;  
  6. using System.Web.UI.WebControls.WebParts;  
  7. using Microsoft.SharePoint;  
  8. using Microsoft.SharePoint.WebControls;  
  9. using System.Reflection;  
  10. namespace WebpartPropertySample {  
  11.     [ToolboxItemAttribute(false)]  
  12.     public class CustomPropertyWebpart: WebPart {  
  13.         // Visual Studio might automatically update this path when you change the Visual Web Part project item.  
  14.         private  
  15.         const string _ascxPath = @ "~/_CONTROLTEMPLATES/15/WebpartPropertySample/CustomPropertyWebpart/CustomPropertyWebpartUserControl.ascx";  
  16.         [WebBrowsable(true),  
  17.             WebDisplayName("Custom Property Name"),  
  18.             WebDescription("Enter the name of the phase page. User will be redirected to this page on click of a phase item."),  
  19.             Personalizable(PersonalizationScope.Shared),  
  20.             Category("Sample Custom Properties")  
  21.         ]  
  22.         public string CustomPropertyName {  
  23.             get;  
  24.             set;  
  25.         }  
  26.         protected override void CreateChildControls() {  
  27.             try {  
  28.                 WebPartManager wpManager = WebPartManager.GetCurrentWebPartManager(this.Page);  
  29.                 if (wpManager.DisplayMode == WebPartManager.BrowseDisplayMode) {  
  30.                     CustomPropertyWebpartUserControl control = Page.LoadControl(_ascxPath) as CustomPropertyWebpartUserControl;  
  31.                     if (control != null) {  
  32.                         control.customPropertyWebPart = this;  
  33.                     }  
  34.                     Controls.Add(control);  
  35.                 } else {  
  36.                     Controls.Add(new LiteralControl("Webpart in edit mode"));  
  37.                 }  
  38.             } catch (Exception ex) {  
  39.                 Controls.Add(new LiteralControl(ex.Message + "<br/>" + ex.StackTrace));  
  40.                 ULSLogger logger = new ULSLogger();  
  41.                 logger.WriteLog(ex.Message + "\n" + ex.StackTrace, Assembly.GetExecutingAssembly().FullName, "WebpartPropertySample - CustomPropertyWebpart");  
  42.             }  
  43.         }  
  44.     }  
  45. }  

Build the WebPart and deploy it to your server.

Add a new page in your SharePoint site or take an existing page.

Add the new WebPart – CustomPropertyWebpart to the page.

Right click WebPart and choose “Edit the WebPart” option.

On the right hand side you will see the WebPart properties.

 
At the bottom of the section, you can find your custom property - CustomPropertyName under the group name - Sample Custom Properties.

 

Go ahead and set value for your custom property.

Happy SharePoint coding!