What a SharePoint Timer Job is
A Timer Job is a periodically executed task inside SharePoint Server. It can do various tasks within the SharePoint environment on a scheduled time event basis.
Create Timer Job step-by-step
- Open the Visual Studio 2012.
- Click New Project.
- Select the Empty SharePoint Solution Template.
- Select Deploy as Farm Solution.
- Add a new class to the project and provide the name (TimerJobDemo.cs).
- To do this, right-click on the project name in Solution Explorer, select Add -> New Item. Under the C# category select the Code tab, select Class and provide the class name. TimerJobApplication -> Add -> New Item -> Class.
- Derive this TimerJobDemo from the SPJobDefinition class.
- You need to add the following statements to do this.
- using Microsoft.SharePoint;
- using Microsoft.SharePoint.Administration;
- public class TimerJobDemo:SPJobDefinition
- {
- }
- Add the following three constructors to your class.
- public TimerJobDemo():base()
- {
- }
-
- public TimerJobDemo (string jobName, SPService service) : base(jobName, service, null, SPJobLockType.None)
- {
- this.Title = "Demo Timer Job";
- }
-
-
- public TimerJobDemo(string jobName, SPWebApplication webapp) : base(jobName, webapp, null, SPJobLockType.ContentDatabase)
- {
- this.Title = "Demo Timer Job";
- }
- Override the Execute() method to do your stuff. Whenever the timer job executes, it will run the code written inside the Execute() method.
- SPWebApplication webApp = this.Parent as SPWebApplication;
- SPList taskList = webApp.Sites[0].RootWeb.Lists["TimerLists"];
- SPListItem newTask = taskList.Items.Add();
- newTask["Title"] = "Job runs at " + DateTime.Now.ToString();
- newTask.Update();
Register the Timer Job Definition
Use the following the procedure to register the Timer Job Definition:
- Add a new feature to register our custom timer job.
- Right-click on Features inside the project in the Solution Explorer and click "Add Feature".
- Rename this feature to TimerJobFeature.
- Select the scope of the Timer job as “WebApplication”.
- Open the Feature properties and set “Active On Default” to “False”.
- Right-click on the “TimerJobFeature” and select “Add Event Receiver”.
- Uncomment the FeatureActivated and FeatureDeactivating methods and write the code as in the following.
- const string JobName = "Demo Job";
- public override void FeatureActivated(SPFeatureReceiverProperties properties)
- {
- try
- {
- SPSecurity.RunWithElevatedPrivileges(delegate()
- {
- SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent;
- DeleteExistingJob(JobName, parentWebApp);
- CreateJob(parentWebApp);
- });
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- private bool CreateJob(SPWebApplication site)
- {
- bool jobCreated = false;
- try
- {
- TimerJobDemo job = new TimerJobDemo(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;
- DeleteExistingJob(JobName, parentWebApp);
- });
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
Deployment and Debug
- Right-click on the project name in your Solution Explorer and select Deploy.
- After successfully deploying it, activate the feature .
- Open Central Administration and select Manage Web Application.
- Select your web application.
- Select the "Manage Feature" tab on the ribbon control.
- The pop up window will display all your application scoped features. Find the feature that we have created here and activate it.
- After activating the feature, the timer job will be registered. To see a list of all the registered timer jobs, go to Central Administration and select "Monitoring->Review job definitions".
- Select the web application and find the job.
Timer Job Debugging
- For debugging the Execute() method, press CTRL+ALT+P and select the process "OWSTIMER.EXE" and click Attach.
Note: Whenever you make any code changes and re-deploy the solution, restart the Timer Service too. This will reduce your debugging time with strange issues.
Output
Conclusion
This article explained SharePoint timer jobs and the step-by-step development deployment and debugging of them.