One of the great aspects of developing custom solutions on SharePoint is the ability to create timer jobs. Rather than creating a Windows Service or Windows Scheduled Task, using a SharePoint timer job makes it simple to keep the timer job a part of your custom SharePoint solution and it lets SharePoint manage the job. Adding a custom timer job to your custom SharePoint solution is simple.

I won’t bore you with how to setup a Visual Studio project for SharePoint 2010, I assume you already know how. With your custom SharePoint 2010 project open in Visual Studio:

  1. In the Solution Explorer right-click the project name (root of your project) and select Add > Class.

  2. Provide a name for your timer job class (e.g. “MyTimerJob.cs”).

  3. Add the following two references to your class:

  4. Add the SPJobDefinition base class to your class:

  5. Add the following code to define your timer job and provide the code to execute when the job runs:

  6. Right-click the Features folder in the Solution Explorer and add a new feature. (If you have an existing feature you want to add your job to, skip this step).

  7. Set the scope of the feature to “WebApplication”.

  8. Right-click the feature you added (or your existing feature) and add an Event Receiver.

  9. Uncomment the FeatureActivated and FeatureDeactivating functions.

  10. Add the following code to the FeatureActivated function to create the timer job when the feature is activated. For the schedule, you can choose other increments (e.g. Daily).

  11. Add the following code to the FeatureDeactivating function to remove the timer job when the feature is deactivated.

That’s it. Deploy your solution and you will now see a timer job in Central Administration for the web application you deployed the project to.

Pin It on Pinterest

Share This
%d bloggers like this: