Click here to Skip to main content
15,512,456 members
Articles / Virtualization
Posted 4 Apr 2006


34 bookmarked

Fundamentals of SharePoint Web Parts (Part I)

Rate me:
Please Sign up or sign in to vote.
3.24/5 (8 votes)
25 Sep 20065 min read
An article on Programming and Deploying SharePoint Web Parts

Sample Image - webshot1.jpg


Since I have been using Code Project as a reference for several years now, I finally decided to make a few contributions of my own. I have been writing some web part components for SharePoint for about two years and while it is not something I do on a regular basis, I have made a few accidental discoveries that I will share with the readers of Code Project.

Some of the Microsoft SharePoint Documentation is not absolutely clear and clarifying this is one of my goals for this article. I also decided that one of the best, most stable, and visible resources on the web for Microsoft Programmers is the Code Project and thus far Code Project has lacked many articles on SharePoint Technologies Web Parts. That being one of my main interests, I decided I would help fill that void as best as I could manage.

This article will be fairly short as I have planned a companion article on deploying Web Parts to be followed by an article on Web Part Communications. The Web Part Communications functionality is one that really makes the development of Web Part Components useful.

So what are Web Parts in relationship to SharePoint, Windows SharePoint Service or SharePoint Portal Services?
SharePoint is a Portal Package that runs in IIS and utilizes the ASP.NET framework to accomplish its basic functionality. So an ASP.NET developer is a logical candidate for fulfilling the role of Web Part Developer. The development of Windows SharePoint Services was inspired by the first Wiki, the Portland Pattern Repository.

Web Part Components compile to .NET Libraries (.dll) and require a filename.dwp in order to be successfully deployed in the site's Web Part Gallery. Additionally if you decide to start your own Web Part from scratch, you will need to add a WP Manifest (Manifest.xml) and a web part dwp (filename.dwp) file in order to successfully deploy a component. If you intend to deploy, you should add a new cab file project to your Web Part Solution. That way you can make use of the ease of deployment via the staadm.exe utility.

Background (Required Software and Environment)


  • Windows 2003 Server
  • Windows SharePoint Services v2.0
  • Visual Studio 2003
  • Web Part Templates

Optional (highly recommended)

Aspects of the code

The WebPartRender and CreateChildControls Method are key to producing output in SharePoint. Without utilizing them or by using them incorrectly, you will see nothing after the initial deployment.

Web Parts utilize the output.Write(""); and ControlName.RenderControl(output); methods to render html/text and WebControls. The CreateChildControls method is useful for instantiating WebControls and any attributes of the particular WebControl. The WebPartRender method is most useful for html and text output formatting but is also required in order to produce output of controls.

Web Parts require a reference to the SharePoint libraries in order to function (see code below). Other references should be added dependent on extended functionality required for the Web Part that you might want or need to develop. All Web Parts will need a companion .dwp file in order to be visible in the Sites Web Part Gallery. Most of what constitutes a good or useful Web Part will be familiar to any ASP.NET developer.

//Sample Code
using System;
using System.ComponentModel;
using System.Data;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using System.Security;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

namespace AKWebPart
/// <summary>
/// Description for AKWebPart.
/// Simple Web Part
/// </summary>
ToolboxData("<{0}:AKWebPart runat=server></{0}:AKWebPart>"),

public class AKWebPart : Microsoft.SharePoint.WebPartPages.WebPart
//Quick Note: The names for the textboxes used are geared
//towards connecting to the northwind sample database
private System.Web.UI.WebControls.TextBox TextBoxID;
private System.Web.UI.WebControls.TextBox TextBoxLastName;
private System.Web.UI.WebControls.TextBox TextBoxFirstName;
private System.Web.UI.WebControls.TextBox TextBoxAddress;
private System.Web.UI.WebControls.TextBox TextBoxCity;
private System.Web.UI.WebControls.TextBox TextBoxRegion;
private System.Web.UI.WebControls.TextBox TextBoxCountry;
private System.Web.UI.WebControls.Button connectButton;

/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void RenderWebPart(HtmlTextWriter output)
    output.Write("<table border=0 id='EmpTable' bgcolor='#DFDFDF/'>");
    output.Write("<fieldset dir='ltr' id='DataFieldset' 
    style='border-style:solid; border-width:1px;
          width: 623px; height: 404px; padding-left:4px; 
    padding-right:4px; padding-top:1px; padding-bottom:1px'>
      <legend><font face='Arial'>Data Fields</font></legend>");

protected override void CreateChildControls()
    TextBoxID = new TextBox();
    TextBoxID.ID = "TextBoxID";
    TextBoxID.Text = "0";
    TextBoxLastName = new TextBox();
    TextBoxLastName.ID = "TextBoxLastName";
    TextBoxFirstName = new TextBox();
    TextBoxFirstName.ID = "TextBoxFirstName";
    TextBoxAddress = new TextBox();
    TextBoxAddress.ID = "TextBoxAddress";
    TextBoxCity = new TextBox();
    TextBoxCity.ID = "TextBoxCity";
    TextBoxRegion = new TextBox();
    TextBoxRegion.ID = "TextBoxRegion";
    TextBoxCountry = new TextBox();
    TextBoxCountry.ID = "TextBoxCountry";
    connectButton = new Button();
    connectButton.ID = "ConnectButton";
    connectButton.Text = "Submit";
    connectButton.Click +=new EventHandler(CellButtonClicked);

private void CellButtonClicked(object sender, EventArgs e)
    //Non Functional here but it does fire a postback event

Below is the basic syntax of the filename.dwp in this case AKWebPart.dwp. The Assembly tag basically references the namespace of the component.

<!-Sample dwp file-->
<?xml version="1.0" encoding="utf-8"?>
    <WebPart xmlns="" >
    <Title>AK Web Part</Title>
    <Description>A silly little demonstration Web Part</Description>
    <!-- Specify initial values for any additional base 
        class or custom properties here. -->

<!--Sample Manifest.xml file-->
<?xml version="1.0"?>
    <!-- You need only one manifest per CAB project for Web Part Deployment.-->
    <!-- This manifest file can have multiple assembly nodes.-->
    <WebPartManifest xmlns="">
        <Assembly FileName="AKWebPart.dll">
        <!-- Use the <ClassResource> tag to specify resources like image files 
             or JScript files that your Web Parts use. -->
        <!-- Note that you must use relative paths when 
            specifying resource files. -->
         <ClassResource FileName="Resource.jpg"/>
    <DwpFile FileName="AKWebPart.dwp"/>

Points of Interest

The simplest way to deploy a web part is to use the stsadm.exe utility. Using STSADM automates a few tasks that would otherwise need to be performed by hand. Setting the appropriate trust level for a web part is accomplished automatically for you with STSADM. If you were to do this by hand, you would need to do several things and in multiple locations in order for your Web Part to function. First the physical file web.config located in the file system root of an SharePoint Site would need to have an entry added for the component.
For example: In the safecontrols section of the web.config c:\inetpub\wwwroot\web.config, you would need an entry like below.

<SafeControl Assembly="AKWebPart, Version=, Culture=neutral, 
    Namespace="AKWebPart" TypeName="*" Safe="True" />

Next you would need to install your dll in the main web sites bin directory and install the .dwp file in the wpcatalog directory. These are physical directories located in the standard web site file directories.

stsadm.exe is normally located at c:\Program Files\Microsoft Shared\web server extensions\60\bin.

The main web.config file is located in the physical root of the main SharePoint Web Site. Assuming that SharePoint is installed as the root site the location should be c:/inetpub/wwwroot/web.config.

The main locations of critical components of a Web Part deployement are the file locations
c:\inetpub\wwwroot\bin and c:\inetpub\wwwroot\wpcatalog

The bin location holds the dlls of a Web Part deployment and the wpcatalog location holds the .dwp file which provides the XML data that is displayed in a web part gallery.

You could install a Web Part by hand by installing the appropriate files in the above named directories, but I really don't recommend it.

For more information on SharePoint, read this article on CodeProject


  • Version one thus far unless I see an error or someone suggests a correction.
  • Added Part II Web Part Deployments. You can view it here.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
Software Developer MHA Of Westchester
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralUpdates added Pin
_alank5-Apr-06 7:17
_alank5-Apr-06 7:17 
GeneralRe: How Long? How Short? How Much? Pin
_alank6-Apr-06 9:04
_alank6-Apr-06 9:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.