Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / productivity / SharePoint

Tip: Debugging a SharePoint 2010 Timed Job

5.00/5 (2 votes)
9 May 2011CPOL2 min read 24K  
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)