Create Visual Web part with Custom Properties in SharePoint 2013

Now, Lets see how to create custom web parts to a visual web part in SharePoint 2013.

If you used new Visual Web Part Template in Visual Studio 2012 to Create Custom WebParts you have first learn about the basic structure of the Visual Web Part in SharePoint 2013.Creating custom properties in SharePoint 2013 is different from creating a Visual Web Part with custom properties in SharePoint 2010. In SharePoint 2013, there is no separate User Control Class( webpartname.cs ) and only web part class (webpartname.aspx.cs) is available. we can directly write custom properties in User Control Class.

following is the SharePoint 2013 Visual Web Part architecture

SharePoint 2013 Custom Properties

Now, follow below steps to create a Visual Webpart with Custom Properties in SharePoint 2013. I will show how to create a drop down menu in this article. similarly, you can also create textbox, boolean value,check box etc.
Refer MSDN for Different Custom Properties.

Add Custom Properties to a Visual Web Part:

  • Add a Visual Web Part to your project.
  • Create a new property by adding the below code in your user control class(webpartname.aspx.cs).
Code:

public enum CustomProp
 {
  AllProjectPortals,
  MyProjectPortals
 }

 [WebBrowsable(true),
 Category("Custom Configurations"),
 Personalizable(),
 WebDisplayName("WebPart Properties"),
 WebDescription("Select a properties to change the WebPart display")]

 public CustomProp WebPartProperty { get; set; }

Using this property in Code behind:

 protected void Page_Load(object sender, EventArgs e)
 {
  if (!Page.IsPostBack)
   {
     lblProjects.Text = this.WebPartProperty.ToString();
   }
 }

Build and deploy the solution.
  • Add the Web Part to a Page and Edit the Web Part to see properties.

SharePoint 2013 Custom Properties
  • Click OK, You can see your Custom Properties displayed on your page.
Hope this is helpful for you.
Create Visual Web part with Custom Properties in SharePoint 2013 Create Visual Web part with Custom Properties in SharePoint 2013 Reviewed by Sudheer Gangumolu on January 10, 2015 Rating: 5

4 comments:

  1. It would be great if you provide JavaScript solution for the same as it's trend of sandbox solution after release of SharePoint2013

    ReplyDelete
  2. Thanks for taking time to check my site, we are trying to provide you the same in the coming days.

    ReplyDelete
  3. You put a 25K deposit down on each property and a mortgage for the remaining 75K, spending all your 100K across 4 properties not just 1 property this time. deyaar projects

    ReplyDelete