Create Timer Job In SharePoint-2010
A
Timer Job is used to execute the tasks which needs to be execute periodically inside SharePoint Server. It can perform various tasks
within the SharePoint Farm on a scheduled time.
Components
of Timer Job :
•Derive CustomTimerJob Class from SPJobDefinition.
•there are three Constructors of the derived class which will execute when ever the
object of CustomTimerJob class is created.
•Override
the Execute method: Execute method will run when ever the timer job start running. this method contains the logic.
• To add Timer Job in SharePoint farm or to remove time job from SharePoint, we can use the feature and feature receiver. So, we need to create a Feature and Feature Receiver.
Timer
Class(Timer Job Definition)
Part-1 Derived from SPJobDifinition
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
public class TimerJobDemo:SPJobDefinition
{ }
Part-2
There are three Constructor
public TimerJobDemo():base()
{
}
public TimerJobDemo (string TimerjobName, SPService service) : base(jobName, service, null, SPJobLockType.None)
{ this.Title = "Demo Timer Job"; }
public TimerJobDemo(string TimerjobName, SPWebApplication webapp) : base(jobName, webapp, null, SPJobLockType.ContentDatabase)
{ this.Title = "Demo Timer Job"; }
Yellow
color constructor represents a default constructor.
Light
blue color constructor represents web
service association constructor .
Green
color constructor represents the service
application
association constructor .
Part-3
Execute Method-
Override
the Execute() method to do your stuff.
public override void Execute(Guid targetInstanceId)
If Lock type is 'ContentDatabase', the 'targetInstanceId' parameter means the GUID of the content database that need to replace the 'targetInstanceId' parameter. Otherwise no need to mention this parameter. this method contains the whole logic that will performed by timer job to complete the business requirement.
Discussion-
• Need to create default (parameter-less) constructor for deserialization purposes.
The 'SPJobDefinition' class is a inherit class from the SPPersistedObject class.
If a default parameter-less constructor is not implement in time job, error message as mentioned below will received during debugging when we call .Update() in timer job.
Error Message- TimerJobClassName cannot be deserialized because not having a
public default constructor.
There
are four values that we need to mention in constructor that are given below.
•Web Application or Web Service
Association Reference
•Server Association Reference (optional)
•Name of the timer job
•Lock Type of the Job
LockType
We have to define SPJobLockType value in the timer job instance constructor that value helps to identified where and how many times the timer job will run. There are three type of Locks values which are mentioned below.
None- means there are no locks applied on this timer job. It will run only one time on each SharePoint servers in farm. but if the parent web application or
service application with which the timer job is associated has been provisioned
to that server.
Content Database- Indicates that the timer job will run once for each content
database associated with the web application with which the timer job is
associated. If one content database is associated with web application, it will
run once. If multiple content databases like 10 are associated, then
it will run ten times. When you specify this option,
the targetInstanceId parameter on the Execute method will
be populated with the GUID of the content database for which the timer job is
firing.
Job- Indicates that the job will run only one time.
Scope
of timer job
•Farm- Activated feature for entire server
farm.
•Site- if scope is Site, timer job feature need to be activated at site collection. Timer job will work for the all subsite under the this site collection.
•Web- Timer Job feature need to be activated at the subsite level.
•WebApplication- Activated a feature for all web sites
in a web application.
Generally we set web application scope to feature.
Note- In SP 2013, Timer Job has four different type of scope as mentioned above but in SP 2010, timer job has two scope that are Site collection and Web Application.
•Feature
Properties- there are two important properties which is need
“Active On Default” must be set to “False”
“ Always
Force Install”
generally set to “True”
Comments
Post a Comment