Developing Sharepoint 2010 Web Parts and Controls

Developing Web Parts and Controls

Web Parts are like user controls provide facility to users to directly modify the content, appearance, and behavior of SharePoint site pages by using a browser. These are server-side controls that run inside a special type of page called a Web Part Page. These are the building blocks of pages that appear on a SharePoint site.

When we add a Web Part item, Visual Studio creates a folder in our project. Several files are added to those folders. Following are default files and their description:

File Name Description
Code File It contains methods which are used for the Web Part to generate custom content.
Elements.xml It contains information that is used by the Feature definition file in the project to deploy the Web Part.
.webpart file It provides information that SharePoint needs to display on Web Part in a Web Part gallery.

Creating a Basic Web Part

It is a simple Web Part that enables the user to define a custom message that is displayed inside the Web Part. This Web Part derives from the Microsoft ASP.NET Web Part class.

Go to visual studio 2010 create new SharePoint Project then add a WebPart.

Put Its name ShowMessage.

Create the Web Part property

1. In the ShowMessage file, copy and paste the following code to create a basic customizable property.

private string sMessage = "Hello, Folks!";

public string ShowMessage

{

     get { return sMessage; }

     set { sMessage = value; }

}

 
2. Add the following tags above the public declaration.

[WebBrowsable(true),

WebDescription("It Displays a Message),

WebDisplayName("Its Display Message"),

Personalizable(PersonalizationScope.User)]

3. Now, add functionality to Web Part. By overriding the CreateChildControls method.
 

protected override void CreateChildControls()

{

    base.CreateChildControls();

    LiteralControl message = new LiteralControl();

    message.Text = ShowMessage;

    Controls.Add(message);

}  

Now, Build the project and finally deploy it. After deploying the webPart Project you can add to your SharePoint Web Page.  

Next Recommended Reading SharePoint 2010 Web part Life Cycle