Friday, July 10, 2009

Debug Sharepoint Event Receiver

Debugging is kind of important when developing a new application. In case of event receiver it is more important as errors are not visible unless they are logged in the text files or in the event log. For event receiver, you cannot start application with F5; you need to set the break point in the code, and wait until item is added or deleted from list.

Furthermore, event receiver DLL needs to be in GAC. It cannot be deployed in the bin folder of web application like web part DLL. It means that, for debugging, your PDB (symbols database) has to be in GAC as well. This is what everybody recommends. Since you can only add DLLs in GAC, there is no straight way how to do that. A simple trick which always works is to place PDB file in the bin folder of web application.

This is the how you can debug event receiver without adding PDB file in the GAC.

1) Right click on project properties of Event Receiver project
2) Go to Build tab
3) Change Output path: to c:\Inetpub\wwwroot\wss\VirtualDirectories\$webApplication$\bin\ where $webApplication$ is you web application folder
4) Build project in Debug mode to create the PDB file and DLL in the \bin of web application
5) If GAC has DLL build in the Release mode, uninstall it
6) Copy or install DLL (Debug mode build) file from web application's \bin folder to (GAC) c:\Windows\Assembly folder
7) Reset IIS Server
8) Hit any page on the SharePoint 2007 web application. It will load W3WP.exe.
9) Attach Debugger to W3WP.exe. If you have multiple W3WP.exe select process by user name
10) Perform add, delete or update operation on a list. Debugger will hit first break point. Make sure to replace DLL in the GAC with every code change.

No comments:

Post a Comment