Fully Featured WebPart in SharePoint

Introduction

In this article I am going to explain you that how to create web part properties which is also called as fully featured webpart.

Web Part Property:

The first thing that you can do to enhance your Web Parts is add properties to your Web Part classes. This allows you to provide either Web Part administrators or users of the Web Part the ability to very easily apply custom settings to Web Parts on the page.

Your Web Part properties will render differently, depending on how they are declared.
Primarily, your properties will render a TextBox so that you can type in a text value for your property and it will get saved.

In this article I will add few custom properties with type of text, int, dropdown. To do so I will be using CreateChildControl method.

Sample Program:-

Solution Structure Image

solution structure

The Code:-

using System;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Microsoft.SharePoint;

namespace Home.Project.Solution.WebParts
{
     public class MyDocs : System.Web.UI.WebControls.WebParts.WebPart
     {
         private int loanValue;
 [WebBrowsable(true), WebDisplayName("Loan Value"),
 WebDescription("Enter a loan amount"), Personalizable(PersonalizationScope.User)]
         public int LoanValue
         {
             get { return this.loanValue; }
             set
             {
                 if (value < 0)
                 {
                     this.loanValue = 0;
                 }
                 else
                 {
                     this.loanValue = value;
                 }
             }
         }

         private string sampleText = string.Empty;
 [WebBrowsable(true), WebDisplayName("Sample Text Value"),
 WebDescription("Enter sample text"), Personalizable(PersonalizationScope.User)]
         public string SampleText
         {
             get { return this.sampleText; }
             set{  this.sampleText = value; }
         }

         public enum CompanyType
         {
             IT,
             Banking,
             Insurance
         }
         private CompanyType cType;
 [WebBrowsable(true), WebDisplayName("Company Type Text Value"),
 WebDescription("Select Company"),Personalizable(PersonalizationScope.User)]
         public CompanyType CType
         {
             get { return this.cType; }
             set { this.cType = value; }
         }

         public MyDocs()
         {
             this.ChromeType = PartChromeType.None;
         }
         protected override void CreateChildControls()
         {
             this.Controls.Add(new LiteralControl("My Web Part"));
             this.Controls.Add(new LiteralControl("<br/><hr/>"));
             this.Controls.Add(new LiteralControl("Sample Text = " + SampleText));
             this.Controls.Add(new LiteralControl("<br/><hr/>"));
             this.Controls.Add(new LiteralControl("Loan Value = " + LoanValue));
             this.Controls.Add(new LiteralControl("<br/><hr/>"));
             this.Controls.Add(new LiteralControl("Company Type = " + CType));
             base.CreateChildControls();
         }
         protected override void Render(HtmlTextWriter writer)
         {
             base.Render(writer);
         }
    }

}


Deploy web part using feature:-

Please refer my other article: create and deploy webpart using feature/

OutPut Image:-

wsp builder

Thanks!
Avinash

calendarApril 9, 2012 · cardInfoyen · commentsNo Comments
tagTags: , , , , , , , , , , , , ,  · Posted in: MOSS, SharePoint, WebPart

Leave a Reply

Spam Protection: , required

myworldmaps infoyen