8-Week Microsoft Teams AppDev Accelerator Program
Limited seats available! - Start date: Wednesday, April 16, 2025
Join Today & Save $1,000
Creating Custom SharePoint Timer Jobs (Automated Cron Jobs)
by Andrew Connell

Last updated February 1, 2007
4 minutes read

Share this

Focus Mode

  • Download article resources
  • Feedback & questions

A few weeks back I posted about how you can create custom timer jobs for use in the latest release of SharePoint to perform scheduled tasks. However, considering there have been almost 40 comments to the post in the last three weeks, I figured the post needed some clarification (duh, ya think?).

The first thing you need to do is create a class that inherits from the Microsoft.SharePoint.Administration.SPJobDefinition class. To implement this class, you need to create a few constructors and override the Execute() method, like so:

namespace AndrewConnell.TaskLogger {
  public class TaskLoggerJob : SPJobDefinition{

    public TaskLoggerJob (): base(){
    }

    public TaskLoggerJob (string jobName, SPService service, SPServer server, SPJobLockType targetType)
      : base (jobName, service, server, targetType) {
    }

    public TaskLoggerJob (string jobName, SPWebApplication webApplication)
      : base (jobName, webApplication, null, SPJobLockType.ContentDatabase) {
        this.Title = "Task Logger";
      }

    public override void Execute (Guid contentDbId) {
      // get a reference to the current site collection's content database
      SPWebApplication webApplication = this.Parent as SPWebApplication;
      SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];

      // get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database
      SPList taskList = contentDb.Sites[0].RootWeb.Lists["Tasks"];

      // create a new task, set the Title to the current day/time, and update the item
      SPListItem newTask = taskList.Items.Add();
      newTask["Title"] = DateTime.Now.ToString();
      newTask.Update();
    }
  }
}

As you can see, this job does nothing important but add a new item to a Task list every time it’s executed. Now that you have the job built, you need to get it registered. Unfortunately the only way to do this is through code, but it sure would be one heck of a candidate for a custom STSADM command. Anyway, since we don’t have that today, I like to use the feature activated & deactivated events to install/uninstall my custom jobs.

To do this, you have to create another class that inherits from the Microsoft.SharePoint.SPFeatureReceiver class and implement the FeatureActivated & FeatureDeactivated event handlers like so:

namespace AndrewConnell.TaskLogger {
  class TaskLoggerJobInstaller : SPFeatureReceiver {
    const string TASK_LOGGER_JOB_NAME = "TaskLogger";
    public override void FeatureInstalled (SPFeatureReceiverProperties properties) {
    }

    public override void FeatureUninstalling (SPFeatureReceiverProperties properties) {
    }

    public override void FeatureActivated (SPFeatureReceiverProperties properties) {
      SPSite site = properties.Feature.Parent as SPSite;

      // make sure the job isn't already registered
      foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) {
        if (job.Name == TASK_LOGGER_JOB_NAME)
          job.Delete();
      }

      // install the job
      TaskLoggerJob taskLoggerJob = new TaskLoggerJob(TASK_LOGGER_JOB_NAME, site.WebApplication);

      SPMinuteSchedule schedule = new SPMinuteSchedule();
      schedule.BeginSecond = 0;
      schedule.EndSecond = 59;
      schedule.Interval = 5;
      taskLoggerJob.Schedule = schedule;

      taskLoggerJob.Update();
    }

    public override void FeatureDeactivating (SPFeatureReceiverProperties properties) {
      SPSite site = properties.Feature.Parent as SPSite;

      // delete the job
      foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) {
        if (job.Name == TASK_LOGGER_JOB_NAME)
          job.Delete();
        }
    }
  }
}

Now… to get it working, all you need to do is:

  1. Deploy the strongly named assembly to the GAC.
  2. Reset IIS (required for SharePoint to “see” the new timer job in the GAC).
  3. Create a feature specifying the receiver class and assembly that contains the event receivers.
  4. Install the feature.
  5. Activate the feature.

This sample timer job assumes it’s running within the context of a WSS v3 site that has a list created with the Tasks list template in the root web within the site collection (or, just create a Team Site at the root of your site collection).

Once you activate the feature, it should show up on the Timer Job Definitions page in Central Administration / Operations. It won’t appear in the Timer Job Status page until it’s executed at least one time. Wait for five minutes or so (the schedule this sample is using) to see if it’s running. You should start to see items showing up in the Tasks list in the root site in your site collection.

To deploy the WSP file, simply add it to the solution store using STSADM (using the following command) and then deploy it to the desired site:

stsadm –o addsolution –filename AndrewConnell.TaskLoggerJob.wsp

If you plan to test the timer job attached in that article, please make sure you also uninstall it. I just realized I never uninstalled mine and my Tasks list had over 30,000 items in it from the last few weeks.

Get the Code!

Want a Visual Studio 2005 solution that includes everything you need to create a custom timer job. Note that I used the technique I described in this article to modify the Visual Studio project to create the WSP file for me:

  • TaskLogger_CustomTimerJob.zip (download using the form below)

Or, you can use this Windows SharePoint Solution Package (*.WSP) to deploy the prebuilt timer job used in this article (handles steps 1-4 above):

  • AndrewConnell.TaskLoggerJob.zip (download using the form below)

Download article resources
Want the resources for this article? Enter your email and we'll send you the download link.
Andrew Connell, Microsoft MVP, Full-Stack Developer & Chief Course Artisan - Voitanos LLC.
author
Andrew Connell

Microsoft MVP, Full-Stack Developer & Chief Course Artisan - Voitanos LLC.

Andrew Connell is a full stack developer who focuses on Microsoft Azure & Microsoft 365. He’s a 20+ year recipient of Microsoft’s MVP award and has helped thousands of developers through the various courses he’s authored & taught. Whether it’s an introduction to the entire ecosystem, or a deep dive into a specific software, his resources, tools, and support help web developers become experts in the Microsoft 365 ecosystem, so they can become irreplaceable in their organization.

Feedback & Questions

newsletter

Join 10,000+ developers for news & insights

No clickbait · 100% free · Unsubscribe anytime.

Subscribe to Andrew's newsletter for insights & stay on top of the latest news in the Microsoft 365 Space!
blurry dot in brand primary color
found this article helpful?

You'll love these!

Web Content Management in MOSS 2007: Terminology Explained

Web Content Management in MOSS 2007: Terminology Explained

September 7, 2006

Read now

Web Content Management in MOSS 2007: Terminology Explained

Web Content Management in MOSS 2007: Terminology Explained

September 7, 2006

Read now

Microsoft product build & version numbers for OSS developers

Microsoft product build & version numbers for OSS developers

November 27, 2005

Read now

bi-weekly newsletter

Join 10,000+ Microsoft 365 full-stack web developers for news, insights & resources. 100% free.

Subscribe to Andrew's newsletter for insights & stay on top of the latest news in the Microsoft 365 ecosystem!

No clickbait · 100% free · Unsubscribe anytime.

Subscribe to Andrew's newsletter for insights & stay on top of the latest news in the Microsoft 365 Space!