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
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
ControlName.RenderControl(output); methods to render html/text and
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.
public class AKWebPart : Microsoft.SharePoint.WebPartPages.WebPart
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;
protected override void RenderWebPart(HtmlTextWriter output)
output.Write("<table border=0 id='EmpTable' bgcolor='#DFDFDF/'>");
output.Write("<fieldset dir='ltr' id='DataFieldset'
width: 623px; height: 404px; padding-left:4px;
padding-right:4px; padding-top:1px; padding-bottom:1px'>
<legend><font face='Arial'>Data Fields</font></legend>");
output.Write("</td><td> </td><td> </td></tr>");
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)
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-->
<WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" >
<Title>AK Web Part</Title>
<Description>A silly little demonstration Web Part</Description>
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=220.127.116.11, 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.