SIGN UP MEMBER LOGIN:    
ARTICLE

How to create webpart custom properties in SharePoint 2010

Posted by Vijai Anand Articles | SharePoint February 27, 2011
In this article we will be seeing how to create webpart custom properties in SharePoint 2010.
Reader Level:

In this article we will be seeing how to create webpart custom properties in SharePoint 2010.

Steps Involved:

  • Open Visual Studio 2010.
  • Create an "Empty SharePoint Project".
  • Right click on the solution and click on Add => New Item.
  • Select "Webpart" template from SharePoint 2010 installed templates.
  • Entire solution looks like the following

    share1.gif
     
  • Replace CustomProperties.cs file with the following code.

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 CustomProperties.CustomPropertiesWP
{
    [ToolboxItemAttribute(false)]
    public class CustomPropertiesWP : WebPart
    {
        private string _value;
        Label lblResult;
       Button btnClick;

         [System.Web.UI.WebControls.WebParts.WebBrowsable(true),
         System.Web.UI.WebControls.WebParts.WebDisplayName("Enter the Value"),
         System.Web.UI.WebControls.WebParts.WebDescription(""),
         System.Web.UI.WebControls.WebParts.Personalizable(
         System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared),
         System.ComponentModel.Category("anavijai Custom Properties"),
        System.ComponentModel.DefaultValue("")]

          public string _Value
        {
            get { return _value; }
            set { _value = value; }
        }
        protected override void CreateChildControls()
        {
            lblResult = new Label();
            btnClick = new Button();
            btnClick.Text = "Click";
            btnClick.Click += new EventHandler(btnClick_Click);
            this.Controls.Add(lblResult);
            this.Controls.Add(btnClick);
        }

        protected void btnClick_Click(object sender, EventArgs e)
        {
            lblResult.Text = _Value.ToString();
        }      
    }
}

  • Build and deploy the solution.

  • Go to the SharePoint Site =>Site Actions =>Edit Page =>Editing Tools => Insert =>Web Part =>Categories => Custom =>CustomPropertiesWP.

    share2.gif
     

  • Click on Add.

  • The web part looks like the following with a button.

    share3.gif
     

  • Edit the webpart you could see a new custom category in the webpart properties.

    share4.gif
     

  • Enter the value and click on Ok.

    share5.gif
     

  • In the CustomPropertiesWP click on the button.

    share6.gif


  

Login to add your contents and source code to this article
share this article :
post comment
 

Thank you, great post. Do you know how I could add a label to the web part properties? I need to place a URL on the web part properties which will point the users to a page with configuration informationf or the web part.

Posted by Tawanda Vakisai Jan 13, 2012
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor