Developing A SharePoint Form WebPart – Part 1 – Set Up the WebPart Class

Before accomplishing any custom development, the necessary foundational code must be added to the WebPart in order to set it up for inclusion in the SharePoint environment. You must establish the following references (shown in the code listing) in the main WebPartclass file. Doing so allows WebPart class derivation out of the WebPart base class, procures SQL connectivity using ADO.NET 2.0, allows for definition of WebPart attributes, and tolerates the binding of the appropriate user interface controls for final output by an overridden RenderWebPart method.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Xml.Serialization;
using System.Xml;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using System.Collections;


Supply the appropriate WebPart base class inheritance derivation for the parent class either through System.Web.UI.WebControls.WebParts or Microsoft.SharePoint.WebPartPages. For the Form WebPart, it is advisable to use the new ASP.NET 2.0 base class derivation, therefore set the inheritance to System.Web.UI.WebControls.WebParts.WebPart. Once the class has been defined, call the relevant XML decoration attributes in the XmlRoot element provided by the System.Xml.Serialization namespace by defining a default XML namespace in order to control the serialization of the WebPart properties.

Optionally, define the ToolBoxData attribute with the default Typename so that the control class is concretely defined when using it within an IDE. After modifications, your top level class definition should look like the following:


    [ToolboxData(“<{0}: FormsWebPart runat=server></{0}: FormsWebPart >”)]
    [XmlRoot(Namespace = ” ProfessionalSharePoint2007.FormsWebPart “)]
    public class FormsWebPart : System.Web.UI.WebControls.WebParts.WebPart


Since you have now appropriately modified the class in order to assimilate into the WebPart framework and made the necessary references to draw on ADO.NET 2.0 functionality in order to submit user entered items to the backend SQL data store, the relevant child controls need to be instantiated in order to capture user input.