After deployment, the timer job definition executes your custom code.
The easiest (and more common) way to deploy a timer job is by feature.
When the feature is being activated, a custom code on the "FeatureActivated" method should install the timer job.
This is a complete example of how to create and deploy a timer job definition.
- In "Visual Studio", Create new 'WSPBuilder' project.
- Now, we will add the feature that will install the timer job definition.
- Right click on the project -> 'Add' -> 'New Item'.
- Under 'WSPBuilder' select 'Feature with Receiver'.
- This will create a new feature with a class that implements the feature installation / activation methods.
- When the dialog box appears, type your timer job name and description.
- Finally, select 'WebApplication' in the 'Scope' section.
- * Note: There are few more scopes that you can select.
- "Farm": the feature will be installed on the entire farm.
- "Site": the feature will be installed on a single site collection (and all sub-sites).
- "Web": the feature will be installed on a specific web site (sub-site).
- feature.xml - contains definitions of the feature.
- elements.xml
- TimerJobInstaller.cs - contains override methods for the installation / activation events.
Now, we will create the timer job itself.
Right click on the 'FeatureCode' folder, 'Add' -> 'Class'.
Type the name of your timer job (I've called it with the singular name: 'MyTimerJob.cs').
This will add a simple class. In order that this class will act as timer job, it should inherit the 'SPJobDefinition' class.
Check this complete code of the 'MyTimerJob.cs' class:
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Microsoft.SharePoint.Administration;
- using System.IO;
- namespace WSPBuilderProject1.FeatureCode
- {
- public class MyTimerJob : SPJobDefinition
- {
- public MyTimerJob() : base()
- {
- this.Title = "My Timer Job";
- }
- public MyTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
- : base(jobName, service, server, targetType)
- {
- this.Title = "My Timer Job";
- }
- public MyTimerJob(string jobName, SPWebApplication webApplication)
- : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
- {
- this.Title = "My Timer Job";
- }
- public override void Execute(Guid contentDbId)
- {
- //TODO: write here the code of your job!
- }
- }
- }
This class overrides the method 'Execute' that will execute the job's custom code!
In this method you can write whatever you need.
For example: code that delete items from lists, create sites, copy documents from document libraries, etc...
Now that our timer job is ready, we should return to the feature receiver class ('TimerJobInstaller.cs') and there we should write the code that installs the job.
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Administration;
- using WSPBuilderProject1.FeatureCode;
- namespace WSPBuilderProject1
- {
- class TimerJobInstaller : SPFeatureReceiver
- {
- const string SYNC_JOB_NAME = "My_Timer_Job";
- public override void FeatureActivated(SPFeatureReceiverProperties properties)
- {
- try
- {
- SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
- // make sure the job isn't already registered
- foreach (SPJobDefinition job in webApp.JobDefinitions)
- {
- if (job.Name == SYNC_JOB_NAME)
- job.Delete();
- }
- // install the job
- MyTimerJob myJob = new MyTimerJob(SYNC_JOB_NAME, webApp);
- // Set the schedule - nightly at 23:00 pm
- //SPSchedule customSchedule = SPSchedule.FromString("daily at 23:00");
- //syncJob.Schedule = customSchedule;
- //Set the schedule - every minute
- //SPMinuteSchedule minuteSchedule = new SPMinuteSchedule();
- //minuteSchedule.BeginSecond = 0;
- //minuteSchedule.EndSecond = 59;
- //minuteSchedule.Interval = 3;
- //syncJob.Schedule = minuteSchedule;
- //Set the schedule - hourly between 0 and 59
- SPSchedule schedule = SPSchedule.FromString("hourly between 0 and 59");
- myJob.Schedule = schedule;
- myJob.Update();
- }
- catch (Exception e)
- {
- }
- }
- 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 == SYNC_JOB_NAME)
- job.Delete();
- }
- }
- public override void FeatureInstalled(SPFeatureReceiverProperties properties)
- {
- }
- public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
- {
- }
- }
- }
I've added in comments more ways to schedule the timer job.
Note that I've override also the 'FeatureDeactivating' method - this will delete the job when the feature would be deactivated.
That's it! Our timer job is ready for deployment.
To deploy the timer job, we will need to install & activate the feature. Then - the feature will do the rest (install the timer job through the code we written in the 'FeatureActivated' method).
- First we need to copy the DLL to the GAC.
- Build your project.
- Right click on the project -> 'WSPBuilder' -> 'Copy to GAC'.
- Then we should copy the feature to the FEATURES directory in the 12 hive.
- Right click on the project -> 'WSPBuilder' -> 'Copy to 12 hive'.
- Now we should install and activate the feature.
- Open cmd.exe and type
cd "Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN"
. Then type these 2 commands:- stsadm -o installfeature -filename TimerJobInstaller\feature.xml -force
- stsadm -o activatefeature -filename TimerJobInstaller\feature.xml -url http://il-moss-dev5:6680/sites/site1
You can now see the new installed timer job in the "Central Administration".
Central Administration > Operations > Timer Job Definitions.