I finally completed a project I have wanted to do for a while, which was to create a project template for SharePoint 2010 timer jobs. The SharePoint 2010 Timer Job Item appears in the Add Item dialog when creating SharePoint 2010 solutions in Visual Studio 2010. After you download and install the extension, you can follow this walkthrough to get started. Additional information is available at the end of the walkthrough.
Update (4/13/2011): Found a small error in the AppData class. A new version 1.1 is posted for download.
1. Open Visual Studio 2010 and select File>>New>>Project
2. In the New Project dialog, select to create an Empty SharePoint Project.
3. Make sure to create a Farm Solution when prompted and use the URL for Central Administration as the target.
4. In the Solution Explorer, right click the project and select Add New Item from the context menu.
5. In the New Item dialog, select to add a new Timer Job and name it HelloJob.
6. When the item is added, you will be prompted to provide a name for the Timer Job. Name it Hello.
7. At this point, the project already has simple logging code written into the Worker.cs class, so you can right-click the project and select Deploy from the context menu. At the end of the walkthrough, I'll discuss the advanced capabilities of the Timer Job item.
8. If you deploy from Visual Studio 2010, the feature will be activated on every Web Application in your farm except Central Administration, which is not exactly what you want. Go to the Manage Web Applications page and deactivate the feature manually for all the Web Applications. Then activate it for the Central Administration Web Application.
9. Once the feature is activated, go to Central Administration>>Monitoring. You will see a link to manage the new timer job.
10. Click the link and you will see the management page where you can set the schedule for the job. Select Immediate.
11. Give the job a few minutes to run and then open the ULS Logs. You should see the job has written entries to the logs.
The Timer Job item was written to provide capabilities that are commonly required. The most important of these capabilities is the ability to manage configuration data like connection strings. When creating timer jobs, you can store the configuration data in several places such as the config file for OWSTIMER or a custom file in your feature. The Timer Job item uses the hierarchical object store to manage configuration values.
Managing the configuration values is accomplished through the AppData class. If you examine the class, you will see that it is set up to track the schedule that is selected in the timer job management page. However, you can add your own configuration values to the class.
Setting configuration values for the timer job is done in the timer job management page ManageJob.aspx. Initially, this page only allows you to select the schedule; however, you can easily add custom code to include a place for adding other configuration values. These values can then be saved using the AppData class.
The Timer Job item also provides a custom logging class named Logger. This class provides a LogInfo and LogError clas that you can use to trace the progress of the timer job. You can easily add additional methods for more control over the logging.