Click here to Skip to main content
Licence 
First Posted 11 Apr 2007
Views 31,702
Bookmarked 24 times

Errorhandling in Webparts for SharePoint 2007

By | 11 Apr 2007 | Article
An approach for handling errors in webparts for SharePoint 2007

Introduction

When delivering webparts you need to have an approach on how to handle errors so that the the webpart "dies" in a nice way and can also communicate the problem to the administrators in a controlled way. This article explains the approach I have used. The approach used here is to catch the error in CreateChildControl and then display the error during the rendering in LiteralControls.

Webpart overrides

If you inherit from System.Web.UI.WebControls.WebParts.Webpart you should override the following methods:

  1. CreateChildControls: called by the ASP.NET page Framework to notify server controls that use composition-based implementation to create any child controls that they contain in preparation for posting back or rendering.
  2. Render: renders the control to the specified HTML writer.

Who should catch what we throw...

The problem I have seen is that if you don't handle errors in a controlled way, the whole SharePoint page gets corrupt which is not acceptable. We should have the following goals of how we handle errors:

  1. Die in a graceful way
    If a webpart just throws an error, the Sharepoint page is not usable
  2. Report something back to the end user
    We need to tell the end user that we have a problem
  3. Send more error information for troubleshooting
    We need to have the possibility to have more extensive error reporting for the System Administrator...

My Approach: Coding a webpart

To at least "die" in a controlled way, I have implemented the following approach. I am still missing some good guidelines on how to do Tracing and Event logging inside Sharepoint 2007 from Microsoft.

  1. Create a private variable to store the Exception inside the webpart
    private Exception childException = null;
  2. In CreateChildControls, have a try/catch and catch the error in the childException variable
    try
    {
        base.CreateChildControls();
        .....
    }
    catch (Exception exp)
    {
        HttpContext ctx = HttpContext.Current;
        ctx.Trace.Warn(ex.ToString());
        childException = ex;
    }
    
  3. In Render check using try/catch and catch the error in the childException variable and then display the result in the page by using labels..
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        if (childException != null)
        displayErrorMsg(writer, childException);
        try
        {
            ...
            ...
            ...
        }
        catch (Exception ex)
        {
            displayErrorMsg(writer, ex);
        }
    

    where

    private void displayErrorMsg(HtmlTextWriter writer, Exception ex)
    {
        this.Controls.Clear();
        this.Controls.Add(new LiteralControl(ex.Message));
        if (ex.InnerException != null)
        {
            this.Controls.Add(new LiteralControl(ex.InnerException.Message));
        }
        this.Controls.Add(new LiteralControl());
        base.Render(writer);
        HttpContext ctx = HttpContext.Current;
        ctx.Trace.Warn(ex.ToString());
    }
    

Points of Interest

After working with SharePoint 2007, I miss good information sources regarding creating webparts. Most examples are just of the "Hello World" complexity which is useless if you would like to deliver something for production.

History

  • 11th April, 2007: Created

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Magnus Salgo

Web Developer

Denmark Denmark

Member

Run my own consulting company in Stockholm, Sweden and deliver solutions using C#, .NET, Sharepoint 2007, Microsoft Project Server.
 
I have also been working with document managment software as Livelink from Open Text, BASISplus from Information Dimensions.
 
My bio Magnus Sälgö, Sweden
MSN: salgo60@msn.com
Skype: salgo60

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 Pinmemberdavedfq10:35 11 Oct '11  
GeneralOverly Simplistic Pinmemberozcro22:59 16 Apr '07  
GeneralRe: Overly Simplistic PinmemberMagnus Salgo1:01 17 Apr '07  
You are more than welcome to give me the right direction where to look. I have been looking into what Microsoft published and have seen nothing more than hello world examples with no error handling.
 
Also asking Sharepoint presales people at Microsoft havn't given me any good advices....
So the purpose of this article is to start somekind of discussion and share if someone has anything better.
 
We need some kind of guidelines from microsoft how to report errors or send events.
 
The best source so far is Neimke's book on webparts(not sharepoint webparts) Chapter 9.3 Recover form errors greacefully but his discussion is not what we in Sharepoint land needs...
 
So please if you have more information how to do it the correct way let me know...

 
Magnus Sälgö
Sälgö Consulting AB, Sweden
 

GeneralRe: Overly Simplistic PinmemberMagnus Salgo23:13 17 Apr '07  
GeneralMore about error handling in Sharepoint PinmemberMagnus Salgo1:44 2 Feb '08  
GeneralRe: Overly Simplistic Pinmemberozcro22:49 7 May '07  
GeneralJ2EE/Java app with Sharepoint Pinmembervikram Verma23:16 11 Apr '07  
GeneralRe: J2EE/Java app with Sharepoint PinmemberMagnus Salgo0:06 12 Apr '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 12 Apr 2007
Article Copyright 2007 by Magnus Salgo
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid