![]() |
Enterprise Systems »
SharePoint Server »
Web Parts
Intermediate
Errorhandling in Webparts for SharePoint 2007By Magnus SalgoAn approach for handling errors in webparts for SharePoint 2007 |
C# 2.0, Windows, .NET 2.0, .NET 3.0, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
If you inherit from System.Web.UI.WebControls.WebParts.Webpart you should override the following methods:
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. Render: renders the control to the specified HTML writer. 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:
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.
Exception inside the webpart
private Exception childException = null;
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;
}
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());
}
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.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 11 Apr 2007 Editor: Deeksha Shenoy |
Copyright 2007 by Magnus Salgo Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |