August 13, 2007 10:27 PM

Custom Web Part Basics

SharePoint Pro
InstantDoc ID #103721
Rating: (0)

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);
}
}
}
}

Related Content:

ARTICLE TOOLS

   
Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here
   

Dan Holme's Viewpoint on SharePoint Blog

Office 15 Technical Preview… And Penguins

Microsoft announced the Office 15 Technical Preview on January 30--here's what it is, why you might...

SharePoint Pro: By Admins, Devs, & Industry Observers

5 Steps to Making SharePoint Information Architecture Work for You

To make SharePoint work for you, a strong base of information architecture offers the structure...

Open Text Sponsored Blog

Governance: It Doesn’t Matter What, It Really Matters Who

Over the years one of the many things I’ve been involved with is governance. To most the word...