Tip: Debugging a SharePoint 2010 Timed Job

Recently, I had to develop a timed job (an instant one-time job) on the SharePoint 2010. During the development, I asked myself how I can debug the just created job, so I started searching for a solution.

03 May 2011
Christoph Keller Christoph Keller

Recently, I had to develop a timed job (an instant one-time job) on the SharePoint 2010.

During the development, I asked myself how I can debug the just created job, so I started searching for a solution.

First, the creation / instantiation of the timed job executes on behalf of the currently loggedin user and is executed in the current web-request. So the debugging of this part of the code is quite simple:

  • Compile and deploy your code to the SharePoint 2010 server
  • Attach the VisualStudio debugger to the corresponding w3wp.exe process (I allways attach it to all the w3wp.exe processes), tip: (thanks to Nemanja Trifunovic ( ^)) you can add the "CommandLine" column to the Task Manager (or even better, use Process Explorer instead) and look for the w3wp.exe instance that has 
    -ap "<app-pool name of your sharepoint instance>"
    in the command line parameters.
  • Add a breakpoint to the instantiate method or to the constructor of your SPJobDefinition class
  • Start the code

Here a screenshot of the Add to process window to select multiple w3wp.exe processes:

This way, you can debug the creation of your timed job.

Now the main issue is, how you can debug the job-processing self. For this, you have to consider some points:

  • The execution of your job is running in a different process (owstimer.exe)
  • The job is called asynchronous, so you cannot define exactly when (at which second) the job is running
  • If the service "SharePoint 2010 Timer" is not running on the machine, the job will never be executed
  • Additionally (perhaps a bug): Sometimes, the breakpoints are not breaking, even if you attached the correct source to the correct process. In this case, you have to restart the "SharePoint 2010 Timer" service and everything works fine.

So to debug the job-processing, you have to follow these steps:

  • Compile and deploy your code to the SharePoint 2010 server (as usual)
  • Not always necessary, but I do it every time: Restart the "SharePoint 2010 Timer" service (more information about restarting services: http://technet.microsoft.com/en-us/library/cc736564%28v=ws.10%29.aspx), thanks to Prasham
  • Add a breakpoint to the Execute(Guid) method in your SPJobDefinition
  • Start the code

Here is a screenshot too about the attaching to a process, but this time, the owstimer.exe is selected:

This way, you can easily debug your SPJobDefinition code.

Most of the informationabout this article is from these two sites, thank you for this!:

http://www.c-sharpcorner.com/Blogs/4415/problem-while-debugging-sharepoint-timer-job.aspx

http://msdn.microsoft.com/en-us/library/ff798310.aspx

I hope this helps anyone. Please feel free to write any comment or question!

Happy coding!


comments powered by Disqus