SUBSCRIBE to SharePointPro Connections     Register today for your FREE "SharePointPro Connections UPDATE " eNewsletter

     

 

     
Skip Navigation Links.
Collapse SharePoint SharePoint
Collapse SharePointPro Connections MagazineSharePointPro Connections Magazine
Migrating to SharePoint 2010
Creating Office Business Applications in Microsoft SharePoint 2010
SharePoint 2010: A First-Class Developer Platform
Using Silverlight 4 Web Parts in SharePoint 2010
7 SharePoint Sleeper Features
Top 5 Things Admins Can Do to Prepare for SharePoint 2010
SharePoint 2010's Sandboxed Solutions
SharePoint 2010: Developer's Perspective
Expand Installation and DeploymentInstallation and Deployment
SharePoint Extends a Nonprofit’s Reach
How to change your personal information in MOSS 2007
KPI's in Microsoft Office SharePoint Server 2007
Expand Integrating SharePoint and Microsoft Office 2003Integrating SharePoint and Microsoft Office 2003
Diving Into the Windows SharePoint Services 3.0 API
Hide custom list items
Linking to documents in another document library
Custom Web Part Basics
Expand Integrating SharePoint and Microsoft Office 2007Integrating SharePoint and Microsoft Office 2007
Testing Our Web Part Base Class
Expand Working OfflineWorking Offline
Installing Microsoft's Application Templates
Manage quick menu item using EditControlBlock in WSS 3.0
Expand Windows SharePoint Services Document LibrariesWindows SharePoint Services Document Libraries
Creating and Using a New Column Type
Corporate Blogging
SharePoint 2007 Content Types
Windows SharePoint Services Out of the Box
More About SharePoint 2007 Content Types
Using Content Types in Windows SharePoint Services 3.0
SQL storage planning & monitoring (MS white paper)
Use Kerberos to Secure MOSS 2007
Display the user name for the logged on user
Outlook 2007 and SharePoint Synchronization
10 Important Kerberos Facts
Stsadm
SSRS and MOSS 2007
Shared Tasks Lists with SharePoint and Outlook 2007
Introducing the Business Data Catalog
Information Integration: SSRS and MOSS 2007
What Can I Accomplish with Other SharePoint Technologies?
Integrate SharePoint into Your Exchange Environment
Outlook and SharePoint: Playing Well Together
SharePoint Integration with Outlook 2007, Part 3
Bridge the SharePoint File-Restore Gap
Migration Glitch in SharePoint Portal Server
Windows SharePoint Services 3.0 Out of the Box
SharePoint Security Evolution
Creating and Using a New Content Type in SharePoint 2007
Expand SharePointPro Connections Update SharePointPro Connections Update
Expand Office 2007Office 2007
Expand Office 2003Office 2003
     

Visit SharePointProSummit.com

     

     
     

Custom Web Part Basics

Custom Web Part Basics

Submitted By: Bob Mixon - MSD2D SharePoint Community Manager and Managing Director of ShareSquared, Inc.

In last week's newsletter, I began to cover the Windows SharePoint Services 3.0 API.  I'm going to continue down this path within the context of Web Parts.  First I want to cover the basics of creating a Web Part base class that you can use for all your Web Part development efforts.  Because my Web Part base class is somewhat large, I'll need to cover it across a couple of newsletters.  In addition, remember this is what I use, and you don't necessarily have to implement it in the same way.  The primary goal is to wrap up common custom Web Part needs into my own base Web Part class.

So, le'ts get started by creating our new base Web part class and defining its structure:

  1. Create a new Microsoft Visual Studio 2005 Class Library project.  Mine is named S2.Web, but you can name yours anything you wish.
  2. Add a reference to System.Drawing, System.Web and System.Web.UI.WebControls.
  3. Rename Class1 to WebPartBase.
  4. Derive your new WebPartBase from the System.Web.UI.WebControls.WebParts.WebPart class.

Your BaseWebPart class module should look similar to the following:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Web;

namespace S2.Web.UI.WebControls.WebParts
{
public class WebPartBase :
System.Web.UI.WebControls.WebParts.WebPart
{
}
}

One of the primary goals our base Web Part class provides is the ability to manage error and informational logging and display.  This is a common need and should be included in every Web Part you create, so why not place the core code in your base Web Part class.

Next we'll add a Label control, which will be a container for error text.  In addition, we'll add a property that indicates whether or not an error has occurred., as the following code shows:

namespace S2.Web.UI.WebControls.WebParts
{
public class WebPartBase :
System.Web.UI.WebControls.WebParts.WebPart
{
private Label _errorLabel = null;

protected bool ErrorOccurred
{
get{ return( _errorLabel != null ); }
}
}
}

Next, we'll add the methods our Web Parts can call when an error occurs.  I've implemented four methods with different signatures, thus allowing me to easily add error information of different types.

namespace S2.Web.UI.WebControls.WebParts
{
public class WebPartBase :
System.Web.UI.WebControls.WebParts.WebPart
{

protected string LogError( string msg )
{
LogErrorInternal( msg );
return( msg );
}

protected string LogError( string module, string proc, string msg )
{
string imsg = String.Format( "{0}.{1} : {2}", module, proc, msg );
LogErrorInternal( imsg );
return( imsg );
}

protected string LogError( Exception ex )
{
string msg = String.Format( "{0} : {1}", ex.Source, ex.Message );
LogErrorInternal( msg );
return( msg );
}

protected string LogError( string proc, Exception ex )
{
string msg = String.Format( "{0}.{1} : {2}",
ex.Source, proc, ex.Message );
LogErrorInternal( msg );
return( msg );
}
}
}

Note that in the above code, I've also made a call to an internal method named LogErrorInternal.  This method takes care of creating a Label control to store our error text.

namespace S2.Web.UI.WebControls.WebParts
{
public class WebPartBase :
System.Web.UI.WebControls.WebParts.WebPart
{


private void LogErrorInternal( string msg )
{
if( _errorLabel == null )
{
_errorLabel = new Label();
_errorLabel.Height = Unit.Percentage(100);
_errorLabel.Width = Unit.Percentage(100);
_errorLabel.ForeColor = System.Drawing.Color.Red;
_errorLabel.Text = msg;
}
else
{
_errorLabel += (Environment.NewLine + msn);
}
}
}

}

The next, and last step, is to override the OnPreRender method.  This method is the last event called before Render in a Web Part's rendering lifecycle.  It's here that we determine whether an error has occurred.  If we find errors have been logged, they are displayed.

namespace S2.Web.UI.WebControls.WebParts
{
public class WebPartBase : System.Web.UI.WebControls.WebParts.WebPart
{


protected override void OnPreRender( EventArgs e )
{
if( _errorLabel == null )
{
base.OnPreRender(e);
}
else
{
this.Controls.Clear();
this.Controls.Add(_errorLabel);
}
}
}
}