SUBSCRIBE to Windows IT Pro Magazine & SAVE 30%     Register today for your FREE 'To The Point' SharePoint eNewsletter
     
     
Skip Navigation Links.
Collapse Office and SharePointOffice and SharePoint
Expand Newsletter ArchivesNewsletter Archives
Expand Office 2007Office 2007
Expand Office 2003Office 2003
Collapse SharePointSharePoint
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)
Display the user name for the logged on user
Outlook 2007 and SharePoint Synchronization
Use Kerberos to Secure MOSS 2007
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
Announcements
     

     

     
     

Testing Our Web Part Base Class

Testing Our Web Part Base Class

Submitted By: Bob Mixon, MSD2D SharePoint Community Manager and Managing Director/ShareSquared, Inc.
Posted On: 3/5/2007

In the tip Custom Web Part Basics, we walked through the steps of creating a simple Web Part base class that you can use in your own common library.  The purpose of creating such a base class is to give you the flexibility to include common features such as error and informational logging, debugging, and so forth.  Before we add any error and information logging features, lets first put our Web Part base class to use by creating and registering a simple Web Part and placing it on a Web Part page.  We can then use this test Web Part as we further enhance the base class feature set.

To get started, open Microsoft Visual Studio (VS) 2005and create a new class library project.  I named mine S2.TestWebPartBase.  This new solution will contain two projects--S2.TestWebPartBase and S2.Web (our Web Part base project from the tip, Custom Web Part Basics).  Your VS solution explorer should look like that in Figure 1:

 

The TestWebPartBase.cs class is a very simple Web Part that's based on our WebPartBase class.  In the source file, implement the following code:

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Text;

using System.Web.UI.WebControls;

using S2;

namespace S2.TestWebPartBase

{

public class TestWebPartBase :

S2.Web.UI.WebControls.WebParts.WebPartBase

{

private Label _label = null;

protected override void CreateChildControls()

{

const string PROC = "CreateChildControls";

int result = 0;

int value = 0;

try

{

// Will throw a 'divide by 0' exception.

// Comment out to see Web Part without exception.

result = 0 / value;

_label = newLabel();

_label.Text = "This is a test";

this.Controls.Add( _label );

}

catch( Exception ex )

{

this.LogError( PROC, ex );

}

}

}

}

Before we can test our new Web Part, we need to copy the S2.Web.dll and S2.TestWebPartBase.dll assemblies to the bin directory associated with a SharePoint Web application.  In addition, we need to register these as safe in the web.config file.

To keep things simple, you can specify a post-build event command line that is executed each time your project is successfully compiled. I recommend that you specify this command line with the SP.TestWebPartBase assembly because this will ensure both assemblies are successfully compiled first (i.e., S2.TestWebPartBase depends on S2.Web, so S2.Web will be compiled first.)

Place the following command line in the S2.TestWebPartBase project post-build event:



xcopy "*.dll" "C:\Inetpub\wwwroot\wss\VirtualDirectories\SSP1\bin" /y

as Figure 2 shows.



Make sure to change the destination bin directory to the location of your SharePoint Web application.  Now build your solution.  After you have a successful build, check the SharePoint Web application bin directory and make sure the xcopy operation was successful.

Now  register these assemblies/namespaces as safe in the SharePoint Web application web.config file.  Before you make any changes to the web.config file, I highly recommend you make a backup copy; any mistakes made in this file will render your SharePoint Web application unusable. Your web.config file should contain two new safe control entries as Figure 3 shows.



If all went well, which I'm sure it has, you should be able to open SharePoint and place an instance of your new Web Part on a Web Part page.  After adding it, you should see the "divide by zero" error message, as Figure 4 shows.



So why do we go through all these steps?  I'm hoping it will all become clear as we move forward with expanding our Web Part base class.  In general, we'll first be expanding on common error handling and logging so that our Web Part can communicate what's happening to our users without delivering a cryptic broken page message.  When something fails in one of my Web Parts, I don't want it to have a negative impact on other SharePoint functionality.