What is Timer Job?
Timer Job
is a background process that is run by SharePoint on specific time intervals.
In Central
Administration, select Monitoring link from the main page, and then select
Review job definitions link under the Timer Jobs section, then you will see the
list of scheduled timer job definitions.
Central
Administration à Monitoring à Timer Jobs à Review job definitions
How to Create Custom Timer Job?
It is as
simple as inheriting a class and override the existing function.
Basic steps
involved in creating and deploying timer jobs are:
1.
Create
a class and inherit from “SPJobDefinition”
2. Ensure that the below namespaces are
added to you file.
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
3. Add the following constructors
public CustomTimerJob() : base() { }
public CustomTimerJob(string jobName,
SPService service):
base(jobName, service, null,
SPJobLockType.None)
{
this.Title = "Your
timer job name";
}
public CustomTimerJob(string jobName,
SPWebApplication webapp):
base(jobName, webapp, null,
SPJobLockType.ContentDatabase)
{
this.Title = " Your
timer job name";
}
4. Override the Execute() method to do your stuff. Whenever timer job executes, it
will run the code written inside Execute()
method.
public override void Execute(Guid
targetInstanceId)
{
SPWebApplication
webApp = this.Parent as
SPWebApplication;
//
write down your code here
}
Registering/Deploying Timer Job Definition
5. Add a new feature to register our
custom timer job.
6. Give Scope this feature to "Web
Application".
7. For this, double click on CustomTimerJobFeature and choose scope "Web Application". Now press F4 so it will open property window
of feature. Select False in "Activate
on Default".
8. Add Feature event receiver for this
feature. Right click on CustomTimerJobFeature and select "Add Event
Receiver".
9. Write the following code to
CustomTimerJobFeatureEventReceiver.cs class.
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
namespace TimerJobApplication.Features.CustomTimerJobFeature
{
[Guid("e6ea0027-1187-419d-b357-306244d0ae37")]
public class CustomTimerJobFeatureEventReceiver : SPFeatureReceiverimer
{
const string JobName = "New Task Timer";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent;
SPSite site = properties.Feature.Parent as SPSite;
DeleteExistingJob(JobName, parentWebApp);
CreateJob(parentWebApp);
});
}
catch (Exception ex)
{
throw ex;
}
}
private bool CreateJob(SPWebApplication site)
{
bool jobCreated = false;
try
{
CustomTimerJob job = new CustomTimerJob(JobName, site);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 15;
job.Schedule = schedule;
job.Update();
}
catch (Exception)
{
return jobCreated;
}
return jobCreated;
}
public bool DeleteExistingJob(string jobName, SPWebApplication site)
{
bool jobDeleted = false;
try
{
foreach (SPJobDefinition job in site.JobDefinitions)
{
if (job.Name == jobName)
{
job.Delete();
jobDeleted = true;
}
}
}
catch (Exception)
{
return jobDeleted;
}
return jobDeleted;
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
lock (this)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent;
DeleteExistingTimerJobFromSite(this.JobName, parentWebApp);
});
}
catch (Exception ex)
{
throw ex;
}
}
}
}
}
10. Deploy the solution and activate the timerjob feature.
11. Go to the Review Timer job page and check your timerjob.
To debug the timer job, follow the below link.
http://jaysp2010.blogspot.in/2015/03/debugging-timer-job-in-sharepoint-2010.html