Click here to Skip to main content
15,885,141 members
Articles / Web Development / ASP.NET

Debugging Custom SharePoint Timer Jobs

Rate me:
Please Sign up or sign in to vote.
4.58/5 (7 votes)
15 Apr 2012CPOL4 min read 90.1K   18   6
SharePoint Timer Jobs Debugging Tricks

Introduction

SharePoint timer jobs are tasks executed on a scheduled basis by the Windows SharePoint Services timer service (owstimer.exe). They are analogous to scheduled tasks, and you can create them in any version of Windows, but SharePoint timer jobs come with many more benefits. SharePoint relies on timer jobs for a number of its functionality areas, and you can even broaden those functionalities and create your custom timer job to introduce new features.

For instance, last month I developed a custom timer job that runs on a daily basis to query all the SharePoint lists and libraries in all the site collections and insert corresponding records into a custom database table for reporting purposes.

Unfortunately, SharePoint timer jobs are tricky when it comes to debugging; it is not about attaching a debugger to w3wp.exe like what we did earlier in this issue. Troubleshooting my custom timer jobs has caused me a lot of headache; that is why I am determined to share with you all the tricks that I have learned in the course of developing a reporting timer job.

To debug custom timer jobs, follow these steps:

  1. Insert an always-failing assertion statement at the beginning of the Execute method. You should use Debug.Assert(false) rather than Trace.Assert(false) since debug calls are detached from the release builds; then you don’t have to remove them from your code before moving to staging or production.

    Image 1

  2. Every time the timer job starts, you will get a pop-up window like the one shown in Figure 12. This window hinders the execution of the timer job until you close the message box. Note that timer jobs run in parallel, so this window will never halt the execution of other timer jobs running simultaneously. For now, leave this window alone.

    Image 2

  3. Select Debug > Attach to Process, and attach a debugger to the Windows SharePoint timer service (owstimer.exe). Make sure that the “Show process from all users” checkbox is selected if owstimer.exe is not listed.

    Image 3

  4. Click the Ignore button in the assertion window.

There you go!

Image 4

You might be wondering why you should insert the assert statement at the beginning of the Execute method of the timer job. It is true that you can just attach a debugger to owstimer.exe and wait for the subsequent cycle of the timer job, but it could be tough to find out whether the application has been effectively attached to the process since the jobs may not fire for several minutes. You could be left with Visual Studio attached to the owstimer.exe process with breakpoints set and wondering whether the job is running or wondering whether the breakpoints are not being hit because of some problem with loading the symbol files.

Cached Assemblies!

Any time you update your custom timer job class and deploy the assembly to the global assembly cache, you must restart all the timer services in the farm. If you don’t restart the timer service, it will run the old copy of your timer job class. Yes, the timer service caches the assemblies. Restarting the SharePoint timer service is the only way to refresh the assembly cache and force it to use your updated assembly. You can achieve that from the command line using the following commands:
net stop sptimerv3 

net start sptimerv3 

Forcing the Execution of Timer Jobs

When you troubleshoot or test your custom timer job, you will probably want to instantly run the job without waiting for the job to run in schedule.

If you have observed the Timer job definitions in SharePoint central administration, you will find that it gives you the capability to disable the job definition but it doesn’t allow you to execute it.

Note: For the out of the box timer jobs, you can use the stsadm –o execadmsvcjobs command, but this will merely execute the administrative timer jobs but it will not execute the custom ones.

One way to work around this is changing the job schedule, deactivating and activating the feature that installs the timer jobs; this will do the trick by altering the Schedule Property of the job. You can use SharePoint Manager 2007 for monitoring the timer jobs status, schedule and the last run time. It’s very useful and time-saving rather than writing custom code to retrieve those values.

Image 5

So a solution to force custom timer jobs to execute is using SharePoint object model as follows. Below is a code snippet that does the trick:

Image 6

Note: Forcing a timer job to execute out of schedule will neither alter the LastRunTime property of the job nor modify the schedule, so don’t let that fool you.

Another way to force the custom timer jobs to run is using ZSTimerJob tool available at CodePlex. The tool is really nifty! It gives the administrators the ability to delete, disable and immediately run timer jobs from the browser without writing a single line of code. MARVELLOUS!

History

  • 6th April, 2010: Initial post

License

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


Written By
Architect
Egypt Egypt
Ayman El-Hattab is a Regional Technology Solution Professional focusing on helping software professionals and organizations build better SharePoint Solutions using Microsoft Application Lifecycle Management (ALM) technologies and tools. Ayman has been in this role since 2010 and has presented at many conferences all over the Middle East & Africa about ALM, SharePoint, C#, asp.net and Business Intelligence technologies. Ayman is also a Microsoft Most Valuable Professional [SharePoint MVP] , ALM Ranger, published author and an enthusiastic speaker who enjoys working with the online and offline developer communities all over the world. Ayman is the founder of MEA ALM Community & SharePoint4Arabs, community lead at Egypt SharePoint User Group and an organizer of several SharePoint Saturday events. Outside of work, Ayman can be found watching soccer games, playing xbox or watching documentary movies.

Comments and Discussions

 
SuggestionFor code purity use Debugger.Launch instead of Debug.Fail Pin
deBUGer!10-Feb-15 22:11
deBUGer!10-Feb-15 22:11 
SuggestionWhy not debug code in a Console App instead? Pin
Jonathan Matthew Beck17-Apr-13 7:22
Jonathan Matthew Beck17-Apr-13 7:22 
QuestionGood One! Pin
Jean Paul V.A12-Jul-12 4:53
Jean Paul V.A12-Jul-12 4:53 
QuestionSharePoint 2010 Debugging Pin
mildkaos5-Jun-12 19:23
mildkaos5-Jun-12 19:23 
GeneralHi. Pin
DFelix18-Apr-11 0:06
DFelix18-Apr-11 0:06 
Hi..can you guide me or provide some links to create a custom timer job with feature to find all Site Collection in the farm.
QuestionHow to create a Custom Timer job in SharePoint? Pin
elizas28-Jun-10 3:56
elizas28-Jun-10 3:56 

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.