Click here to Skip to main content
15,881,715 members
Articles / Productivity Apps and Services / Sharepoint

Tip: Debugging a SharePoint 2010 Timed Job

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
9 May 2011CPOL2 min read 23.3K   1   6
Debugging a SharePoint 2010 Timed Job

Recently, I had to develop a timed job (an instant one-time job) on 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 logged in 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 always 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 is 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 can be found here), 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 information about this article is from these two sites, thank you for this!:

I hope this helps anyone. Please feel free to write any comments or questions!

Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Switzerland Switzerland
I'm a software developer with ASP.NET experience since 2007

Projects:
=========
See my projects list here: http://dotnetcorner.ch/Home/Projects

=========
More about me: http://dotnetcorner.ch
My Blog: http://dotnetcorner.ch/Home/Blog

Comments and Discussions

 
SuggestionWhy debug a timer job? Pin
Jonathan Matthew Beck17-Apr-13 7:22
Jonathan Matthew Beck17-Apr-13 7:22 
GeneralRe: Why debug a timer job? Pin
Christoph Keller17-Apr-13 7:49
Christoph Keller17-Apr-13 7:49 
GeneralAttach to exact w3wp proces Pin
Daniel Popović9-May-11 20:58
Daniel Popović9-May-11 20:58 
GeneralRe: Attach to exact w3wp proces Pin
Christoph Keller9-May-11 21:03
Christoph Keller9-May-11 21:03 
GeneralAttaching to all the w3wp.exe processes Pin
Nemanja Trifunovic4-May-11 2:57
Nemanja Trifunovic4-May-11 2:57 
Not that there is anyting wrong with that, but if you want to attach to the SharePoint one only, 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 "SharePoint Content AppPool"


in the command line.

GeneralRe: Attaching to all the w3wp.exe processes Pin
Christoph Keller9-May-11 20:53
Christoph Keller9-May-11 20:53 

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

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