Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tip: Debugging a SharePoint 2010 Timed Job

, 9 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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 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 at 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 information about this article is from these two sites, thank you for this!:

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

Happy coding!

License

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

Share

About the Author

Christoph Keller
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? PinmemberJonathan Matthew Beck17-Apr-13 7:22 
GeneralRe: Why debug a timer job? PinmemberChristoph Keller17-Apr-13 7:49 
GeneralAttach to exact w3wp proces PinmemberEruf9-May-11 20:58 
GeneralRe: Attach to exact w3wp proces PinmembersT0Ps9-May-11 21:03 
GeneralAttaching to all the w3wp.exe processes PinmemberNemanja Trifunovic4-May-11 2:57 
GeneralRe: Attaching to all the w3wp.exe processes PinmembersT0Ps9-May-11 20:53 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 10 May 2011
Article Copyright 2011 by Christoph Keller
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid