A custom timer job within SharePoint enables you to add a scheduled task to a SharePoint application. This blog post describes how to create and deploy Custom Timer Jobs.
Creating Custom Timer Jobs
Create a Custom Timer Job in SharePoint is very simple, you can declare a class which inherits from SPJobDefinition and overrides the Execute method as shown in the following example :
public class SampleTimerJob : SPJobDefinition
{
public const string JOBNAME = "SAMPLETIMERJOB";
public const string JOBTITLE = "Sample Timer Job";
public SampleTimerJob()
{
this.Title = JOBTITLE;
}
public SampleTimerJob(SPWebApplication webApplication)
: base(JOBNAME, webApplication, null, SPJobLockType.Job)
{
Title = JOBTITLE;
}
public override void Execute(Guid targetInstanceId)
{
// write your code here
}
}
Noteworthy is the SPJobDefinition constructor(s) which accepts a SPJobLockType value. This parameter controls the locking behavior of the timer job and can be one of the following values:
ContentDatabase-Locks the content database. A timer job runs one time for each content database associated with the Web application.
Job-Locks the timer job so that it runs only on one machine in the farm.
None-Provides no locks. The timer job runs on every machine in the farm on which the parent service is provisioned, unless the job is associated with a specified server. In this case, it runs on that server only (and only if the parent service is provisioned on the server).
Based on these options, you must decide which value is correct for your tasks, the SPJobLockType.Job option is most likely the option to use.
DeployingCustom Timer Jobs
After you have created a timer job, the next step is to deploy it. Unfortunately, there is no provision in XML to deploy timer jobs. You can create custom applications, use PowerShell, or create a custom STSADM command to deploy the custom timer job. However, another technique is to use a web application-scoped feature to deploy the custom timer job. In your feature receiver, register the custom timer job as shown in the following example:
public class SampleFeatureReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
if (webApp == null)
{
throw new ArgumentNullException("webApp");
}
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name == SampleTimerJob.JOBNAME)
{
job.Delete();
break;
}
}
SPHourlySchedule schedule = new SPHourlySchedule();
schedule.BeginMinute = 0;
schedule.EndMinute = 59;
SampleTimerJob sampleJob = new SampleTimerJob(webApp);
sampleJob.Schedule = schedule;
sampleJob.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
if (webApp == null)
{
throw new ArgumentNullException("webApp");
}
foreach (SPJobDefinition job in webApp.JobDefinitions)
{
if (job.Name == SampleTimerJob.JOBNAME)
{
job.Delete();
break;
}
}
}
}