June 30
HOW CAN YOU DEBUG YOUR WEB PARTS
Whenever an error occurs in a web part, Microsoft Office SharePoint Server 2007 shows a generic error message. If
you want to see a detailed error message, you should open the web.config file of the SharePoint web application
that contains the web part, locate the <SafeMode> node, and set the CallStack attribute to true. If you want to
be able to see more than a generic server error message, you should locate the <customErrors> element and set
the mode attribute to Off.
You do not need to set the debug property of the <compilation> element to true to be able to debug a web
part. This setting tells ASP.NET to generate symbol tables (.pdb files) when compiling pages. Since your web part
code is contained in an assembly, you can debug the necessary .pdb files by building a Debug version of the web
part library. When the debug symbols are present, you can debug web parts by attaching to the process (an
instance of w3wp.exe).
However, if you want to be able to debug web parts by letting Visual Studio 2005 auto-attach to the process,
you do need to set the debug attribute of the <compilation> attribute to true. In that case, you also need to raise
the trust level for the SharePoint web application containing your web part to WSS_Medium or greater.
Be careful: setting the debug attribute of the <compilation> attribute to true has an undesirable side effect.
Every time you run the web part library project, a .bak copy of your web application’s web.config file is created.
The blog post “Debugging web parts – a full explanation of the requirements,” written by Maurice Prather,
discusses the previously discussed techniques in detail. The blog post can be found at the following location:
http://www.bluedoglimited.com/SharePointThoughts/ViewPost.aspx?ID=60.
Whenever you are auto-attaching to a process in Visual Studio 2005, the web server is restarted in order to
recycle the w3wp.exe process hosting your web part. This is overkill. Instead, it is much faster if you just restart the
application pool that hosts the SharePoint web application containing your web part and then manually attach to the
process hosting your web part. You can restart the application pool via iisapp.vbs (located in the [drive letter]:\windows\
system32 folder) using the following command:
iisapp.vbs /a “[application pool name]” /r
You can make attaching to this process even easier by installing the Debugger feature for SharePoint, created
by Jonathan Dibble. This feature adds a new menu item to the Site Actions menu of a SharePoint site and is
described in detail in the blog post “Debugger ‘Feature’ for SharePoint ” at http://blogs.msdn.com/
sharepoint/archive/2007/04/10/debugger-feature-for-sharepoint.aspx. The feature can be
downloaded from the SharePoint 2007 Features site at CodePlex (http://www.codeplex.com/features/
Release/ProjectReleases.aspx?ReleaseId=2502). This list of free SharePoint features is maintained by
Scot Hillier.