Friday 13 June 2014

SharePoint 2010 visual webpart custom properties


Open Visual Studio 2010 -> File -> New -> Project -> Visual C# -> SharePoint -> 2010 and select Visual Web Part project
Name it: AddCustomProperty
Click Next
Select farm solution
Finish
Expand VisualWebPart1 in VS 2010 solution explorer
Elements.xml, VisualWebPart1.cs, VisualWebPart1.webpart and VisualWebPart1UserControl.ascx files will be added
Elements.xml:
<Property Name="Group" Value="Custom" />
It will says that Webpart will be available in "Custom" group category
Open VisualWebPart1.cs
Add below code
//Web Part property
private string strName;
[WebBrowsable(true), WebDisplayName("Name"), WebDescription("Enter Your Name"),
Personalizable(PersonalizationScope.Shared), Category("Custom Property"),
System.ComponentModel.DefaultValue("")]
public string Name
{
get { return strName; }
set { strName = value; }
}

Open VisualWebPart1UserControl.ascx
Add <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
Open VisualWebPart1UserControl.ascx.cs
public string Name { get; set; }
TextBox1.Text = "Your Name is : " + this.Name;
Final Code for VisualWebPart1UserControl.ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace AddCustomProperty.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
public string Name { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "Your Name is : " + this.Name;
}
}
}
Open VisualWebPart1.cs
Replace CreateChildControls() code with below code
VisualWebPart1UserControl control = (VisualWebPart1UserControl)Page.LoadControl(_ascxPath);
control.Name = Name;
Controls.Add(control);
Final code for VisualWebPart1.cs
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace AddCustomProperty.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public class VisualWebPart1 : WebPart
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @"~/_CONTROLTEMPLATES/AddCustomProperty/VisualWebPart1/VisualWebPart1UserControl.ascx";
//Web Part property
private string strName;
[WebBrowsable(true), WebDisplayName("Name"), WebDescription("Enter Your Name"),
Personalizable(PersonalizationScope.Shared), Category("Custom Property"),
System.ComponentModel.DefaultValue("")]
public string Name
{
get { return strName; }
set { strName = value; }
}
protected override void CreateChildControls()
{
//Control control = Page.LoadControl(_ascxPath);
//Controls.Add(control);
VisualWebPart1UserControl control = (VisualWebPart1UserControl)Page.LoadControl(_ascxPath);
control.Name = Name;
Controls.Add(control);
}
}
}
Bulid the application
F5
You will get sharepoint site
Click on Edit
Click on Insert
Select Web Part
In the custom category, select VisualWebPart1, click Add
Click on Edit Web Part
In the right side window you will able to see "Custom Property", enter name in the text box.
Click OK
IN the Web web part Ui you can see, Your Name is : ******