Add Custom Web Part Properties in SharePoint 2013 Visual Web Part

add custom property visual web part sharepoint 2013

In this post, we will discuss how we can add a custom property to the SharePoint 2013 visual web part as well as how we can set default value also. If you have not gone through steps to create a visual web part in SharePoint 2013 using visual studio 2013, please go through it.

In that web part, I have a label control and I want to set a default value for the label. Also, users should able to change the value from web part properties.

Also, check out:
SharePoint 2013 How to Use LINQ With SPMetal

Web Part Categories missing in SharePoint 2013 or SharePoint 2010

Show List View web part in a visual web part in SharePoint 2010

Here we will deal with three files, one the Visual web part .aspx file as well as visual web part code file. And the web part file (MyTestVisualWP.cs).

Open your Web part file (MyTestVisualWP.cs) and here we need to add properties to the web part like below:

const string const_Name = “EnjoySharePoint”;

private string _yourName = const_Name;

[Personalizable(PersonalizationScope.Shared)]

[WebBrowsable(true)]

[Category(“My Category”)]

[WebDisplayName(“Enter Your Name”)]

[WebDescription(“Provide your name.”)]

[DefaultValue(const_Name)]

public string YourName

{

get

{

return _yourName;

}

set

{

_yourName = value;

}

}

Now your web part file should look like below:

add custom web part properties to visual web part sharepoint
add custom web part properties to visual web part SharePoint

visual web part add custom web part properties

Now Open the web user control file and write like below:

public MyTestVisualWP WebPart { get; set; }

protected void Page_Load(object sender, EventArgs e)

{

this.WebPart = this.Parent as MyTestVisualWP;

lblMessage.Text = “Hello: ” + this.WebPart.YourName;

}

Your code should look like below:

add custom web part properties to visual web part sharepoint
add custom web part properties to visual web part SharePoint

Now deploy your visual web part to the site. Then add the web part to the page and it will show the default value like below:

sharepoint 2013 add custom web part properties to visual web part
sharepoint 2013 add custom web part properties to visual web part

Now edit the page and then edit the web part. You will be able to see the web part properties like below and you change the value like below:

add custom property visual web part sharepoint 2013
add the custom property visual web part SharePoint 2013

Now the changed value will appear like below:

add custom property visual web part sharepoint 2013
add the custom property visual web part SharePoint 2013

Hope this will be helpful to you.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →