Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

SharePoint 2010 – Web Parts Programming

Posted by Paul on June 11, 2012

In this article we can explore Web Parts one of the important extensible tool inside SharePoint. Using Visual Studio we can create Web Parts.

What are Web Parts?

Web Parts are user customizable regions inside a SharePoint web page. It is an ASP.NET custom control which inherits from System.Web.UI.WebControls.WebPart. A Web Part needs the following to work with:

· WebPartZone

· WebPartManager

We can create Web Part using the following ways:

1. Inside SharePoint

2. Using Visual Studio


Creating Web Part using Visual Studio

Now we can try creating Web Part using Visual Studio 2010. Create a new Visual Web Part project from the SharePoint 2010 templates.


In the next page select the site and click Finish button.

Inside the Solution Explorer, open the Visual Web Part User Control. This control represents the User Interface of the web part.


Add a label and change the text of it.



Now build the project and once it succeeds, use the Deploy option of project.


Wait for a few minutes and our Web Part gets deployed into the SharePoint site.

Steps in Manual Deployment

Following are the steps in manual deployment:

1. Create Web Part

2. Sign the Assembly

3. Copy to bin/GAC folder

4. Add to SafeControl list of web application

To our help, Visual Studio does all these activities.

Viewing the Web Part

For viewing the web part use the Edit Pate from any page and access the Add Web Part dialog.


Inside the Custom category we can see the newly created web part. Click on the Add button and you can see the web part as shown below.


Rendering Web Part

The rendering of the web part is done by the class named VisualWebPart1. You can see the CreateChildControls() method inside it.

protected override void CreateChildControls()


Control control = Page.LoadControl(_ascxPath);



Here the User Control (ASCX) is rendered to the web part using Controls.Add() method.

Note: In this example we have just used the label control. We can use the other ASP.NET controls inside web parts.

Kinds of Web Part

There are 2 kinds of web parts:

1. Classic Web Parts (Add new item > Web part)

2. Visual Web Parts (Add new item > Visual Web part)

Tool Parts

Along with the web part, we can associate specific controls in the properties area of the web part. The Edit Web part command brings the Tool window on the right side.


If we add a custom control or property into the above area it is called as Tool Part.

· We are experimenting with adding a Tool part consisting of following controls:
Drop Down List with existing List Names

· Text Box control to capture the Customer Name

Following are the activities involved:

· Add a CustomerName property inside Web Part

· Create a custom class inheriting ToolPart

· Render Controls inside ToolPart

Following are the modifications inside VisualWebPart class:

public override ToolPart[] GetToolParts()


ToolPart[] allToolParts = new ToolPart[3];

WebPartToolPart standardToolParts = new WebPartToolPart();

CustomPropertyToolPart customToolParts = new CustomPropertyToolPart();

allToolParts[0] = standardToolParts;

allToolParts[1] = customToolParts;

allToolParts[2] = new MyToolPart();

return allToolParts;


protected override void Render(HtmlTextWriter writer)







DefaultValue(“Default Name”),



FriendlyName(“Customer Name”),

Description(“The name of the customer”)]

public string CustomerName





Following is the definition of ToolPart class:

public class MyToolPart : Microsoft.SharePoint.WebPartPages.ToolPart


private DropDownList _dropDownList;

protected override void CreateChildControls()


Panel panel = new Panel();

dropDownList = new DropDownList();

_dropDownList.ID = “id2”;

// Populate lists inside DropDownList control

SPListCollection lists = SPContext.Current.Web.Lists;

foreach (SPList list in lists)


TextBox textbox = new TextBox();

textbox.ID = “id3”;






public override void ApplyChanges()


VisualWebPartProject1.VisualWebPart1.VisualWebPart1 webpart = (VisualWebPartProject1.VisualWebPart1.VisualWebPart1)this.ParentToolPane.SelectedWebPart;

webpart.CustomerName = “You selected: ” + _dropDownList.SelectedValue;



Build and Deploy the project and use the Edit Web Part option. You can see the new controls in the Tool Part panel as shown below:


Select a List item and click the Ok button. You can see the List item name displayed in the web part as shown below:


Custom Properties for Web Part

You might have noticed that the Web Part custom property having some attributes. Each of them are described below:

Following is an example code for custom property:

Property Description
Browsable If false, the property is not displayed on the web part property pane
DefaultValue Default Value for the property
WebPartStorage Shared, Personal and None are the enumeration members
FriendlyName The name for the property for display purposes
Description The tooltip about the property



In this article we have explored Web Parts creation, data access, connectable web parts etc. In real world scenarios Web Parts provide a handy tool for the developer. The attachment contains the source code we discussed.


2 Responses to “SharePoint 2010 – Web Parts Programming”

  1. Pascal said

    Can you put your example??? please

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s