What are SharePoint Timer Jobs?
SharePoint timer jobs are tasks executed on a scheduled basis by the SharePoint Timer service.

SharePoint comes by default with a lot of timer jobs, like profile synchronisation between Active Directory and SharePoint. To see all currently installed timer jobs go to Central Administration > Operations > Timer Job Definitions.

Developing your own SharePoint Timer Job
To create your own SharePoint 2007 timer job you have to perform the following steps:

  1. Create a class library project.
  2. Create a class derrived from SPJobDefinition that contains the business logic.
  3. Create a class derrived from SPFeatureReceiver that can be deployed and functions as an installer for the timer job.
  4. Create a feature description file: feature.xml
  5. Create a solution description file: manifest.xml
  6. Create build description files for makecab.exe to build a .wsp (cab) file: .ddf and .Targets
  7. Build the wsp file.
  8. Add the solution to SharePoint.
  9. Deploy the solution.
  10. Activate the feature.

Nobody wants to do all these steps for each new timer job. So in order to make it simpler to start a new timer job project, I created a visual studio template containing all the neccessary stuff you need.

Visual Studio 2005 Project Template
Andrew Connell wrote a good article on his blog on how to create a SharePoint timer job and to modify the visual studio project to get a deployable .wsp file after building the project. Based on his article I created a reusable visual studio template. In addition I included a setup.bat file that can deploy and undeploy the .wsp file.

How to use the template

  1. Download the template and save it in MyDocuments\Visual Studio 2005\Templates\Visual C#.
  2. Create a new project using the “Custom SharePoint Job” template (Be careful to not use a dot in the project name).
  3. Write your logic inside the Execute method.
  4. Build the project.
  5. Run the setup.bat in your project folder to install, deploy and activate your job (Do not copy it into your bin folder, just run it. Everything is taken care for you. :) ).

If no errors are shown your timer job is listed in the Timer Job Definition list of the central administration. Navigating to Timer job status you can find out if your job was executed and if there were any errors. In case of an error exception messages are written to the EventLog by SharePoint.

Tip: Before you start customizing your project (namespace, assembly version, file names) look inside every file to get an overview on how everything fits together.

Good luck on making good (timer-)jobs,
Alexander Brütt

Download: CustomSharePointJob.zip

Deploying the timer on a SharePoint Farm:
I had problems deploying the timer job on a farm. The following commands deployed the timer on the farm correctly:

1. Add the solution
stsadm -o addsolution -filename {WSPFILENAME}
2. Deploy the solution
stsadm -o deploysolution -name {WSPFILENAME} -url {SITEURL}
3. Install the feature
stsadm -o installfeature -filename {FeatureFolder}\feature.xml
4. Activate the feature
stsadm -o activatefeature -id {FEATUREID} -url {SITEURL} -force

kick it on DotNetKicks.com