SharePoint: list running timer jobs (C#)

Role of the timer jobs in SharePoint

Timer jobs are a convenient way to perform background tasks in SharePoint. Writing custom jobs for our solution lets us define job schedule and report progress using a simple API. When the job definition is added to a farm (e.g. from solution code, usually on a feature activation event receiver), we can also manage the job and monitor progress of the running jobs in Central Administration (http://server-name/_admin/ServiceJobDefinitions.aspx).

A look at the architecture

The process that is responsible for executing timer jobs is OWSTimer.exe. Jobs can be associated with either an Web application or Service Application. If you need more information about the architecture and internals of this part of SharePoint, be sure to check the article by Veena Sarda [1].

Jobs can be managed from the dedicated pages in CA or with the use of PowerShell. While the Central Administration view allows us to see the current state of the jobs in the browser window, some of use will need to check jobs status or history in C# code.

Listing running jobs in C#

The function presented below returns names of all the timer jobs currently running in a SharePoint farm. With slight modification it might check for example whether a single particular job is running. Along with the function to determine last run date of a timer job in C# it allows to know precisely about the status of a job at a given time. The code was tested on version 2010.

While it’s quite easy and could be presented as a one-liner, the official documentation is lacking. So far there was much confusion on the subject in the stack exchange answers and top Google results, so I’m just posting it for reference. Most people try to use the HistoryEntries property of a SPJobDefinition and fail, because the entries are only written to the history when timer job terminates–either successfuly or when it’s aborted.

Sources and useful articles

Here are some references on the subject.

  1. SharePoint 2010 Internals – Timer Jobs by Veena Sarda
  2. StackOverflow: Check if a SharePoint timer job is currently running
  3. StackExchange Sharepoint: programatically check status of a job