SharePoint Custom Timer Jobs
We have had a need to create a custom timer job with SharePoint 2007 which performs a daily task that in turn updates data within a SharePoint site. There are many tutorials on the web that show you how to go about doing this but not much in way of information about problems encountered while doing so. So I’m going to use this blog to highlight some of the issues we have come across during development and deployment.
Before I begin describing the issues, the following link has a good tutorial for setting up a Custom Timer Job.
http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx
This is the tutorial that was used to get our custom timer Job up and running and it uses a SharePoint feature to install and schedule the Timer Job.
Issue 1 – Deployment Instability
The method used in the tutorial initially met our requirements for getting the custom timer job into the system however after repeated uninstallations and installations we noticed that the installation process was becoming less and less reliable. The following behaviours were observed and becoming more frequent as development progressed.
- Timer Job installed but would not run
- Timer Job would not install
We spent a lot of time investigating the causes of these issues and we came up with some steps which alleviated (but did not solve) some of the problems.
- Rename the Timer Job
- We found that renaming the custom timer job prior to each deployment reduced the risks of conflicts between a previously installed version of the custom timer job and the newly installed version
- Restart the Windows SharePoint Services Timer Service after each deployment. Restarting the Windows SharePoint Services Timer Service makes sure that the Windows SharePoint Services Timer Service is not using a cached version the Timer Job.
We also encountered some Permissions issues when deploying the custom timer job via a Site Feature which we solved using some work arounds but they are not recommended and will not be discussed here.
Some way through the development there was a requirement to give the user more control of the configuration of the Timer Job. To do this a new process was devised which involved creating a custom action and a custom page which would allow the user to enter configuration information, and adding it as a feature to Central Administration. When the feature is activated the Custom Action adds a link within Central Administration which will take the user to the custom page when clicked.
Issue 2 – Deploying to a Multi-Server Farm
The tutorial above is a good place to start when creating a custom Timer Job however there is one point that is not explicitly made and that is that the code will only work on a Single Server farm. We encountered this problem when we tried deploying the custom timer job onto a Multi-Server farm. The custom timer job worked absolutely fine on our Single Server development environment but when deployed to the Multi-Server environment the job would throw an error when run. After much investigation we discovered that the cause of the error is this line:
public TaskLoggerJob (string jobName, SPWebApplication webApplication)
: base (jobName, webApplication, null, SPJobLockType.ContentDatabase) {
this.Title = "Task Logger";
}
The two highlighted parts represent the SPServer object that the job is running on and the SPJobLockType.
For the Timer Job to run on a Multi-Server farm the above line of code should be changed to:
public TaskLoggerJob (string jobName, SPWebApplication webApplication)
: base (jobName, webApplication, SPServer.Local, SPJobLockType.Job) {
this.Title = "Task Logger";
}
Helpful Tips
Through an abundance of trial and error we have come up with following steps that should be followed to ensure a smooth installation/re-installation of a custom timer job:
- Disable the existing custom timer job
- Uninstall the custom timer job
- Restart the Windows SharePoint Services Timer Service
- Retract the solution containing the timer job definition
- Deploy the solution containing the new timer job definition

