Introduction
In this article, I will explain, how to create a Timer Job in SharePoint 2013 programmatically, using Server-Side Object Model. A timer job runs in a specific Windows Service for SharePoint 2013. Timer jobs perform infrastructure tasks for the Timer service. Timer jobs also perform tasks for the Web Applications, such as sending Email alerts. A timer job contains a definition of the Service to run and specify, how frequently the Service is started.
Pre-Requisites
- Open Visual Studio.
- Open New Project dialog box, expand the Office/SharePoint node and choose SharePoint Solutions.
![Office/SharePoint node]()
- Choose SharePoint 2013 – Empty Project template. Name the project TimerProject and click OK button.
![Empty]()
- Choose Deploy as a farm solution option button and then choose Finish button to accept the default local SharePoint site.
![Deploy]()
- In Solution Explorer, choose TimerProject project, we created.
![Solution Explorer]()
Create Timer Job
- Right click on the Project and select New Folder.
![New Folder]()
- Name the folder as TimerJob.
![TimerJob]()
- Add a class file inside TimerJob folder.
![TimerJob folder]()
- The coding for the class file is given below-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TimerProject.TimerJob {
- class TimerClass {}
- }
![code]()
- Inherit SPJobDefinition class and make the class as public and create the constructors.
- using Microsoft.SharePoint.Administration;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TimerProject.TimerJob {
- class TimerClass: SPJobDefinition {
- public TimerClass(): base() {}
- public TimerClass(string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType) {}
- public TimerClass(string jobName, SPWebApplication webApplication): base(jobName, webApplication, null, SPJobLockType.ContentDatabase) {
- this.Title = jobName;
- }
- public override void Execute(Guid contentDbId) {
- try {} catch (Exception ex) {}
- }
- }
- }
- Next, we need to create a Feature and a FeatureReceiver.
- Add a feature by right clicking on Features folder.
![Features folder]()
- Name the feature. Make the scope of the Feature as WebApplication.
![WebApplication]()
- Add the event receiver to the Feature by right-clicking on the feature name.
![Add the event]()
- Add the code, given below, inside the FeatureActivated Event.
-
- public override void FeatureActivated(SPFeatureReceiverProperties properties) {
- try {
- SPSite site = properties.Feature.Parent as SPSite;
- SPSecurity.RunWithElevatedPrivileges(delegate() {
- using(SPSite elevatedSite = new SPSite(site.ID)) {
-
- foreach(SPJobDefinition job in elevatedSite.WebApplication.JobDefinitions) {
- if (job.Name == "TestTimerJob") {
- job.Delete();
- break;
- }
- }
-
- TimerClass corpProfileJob = new TimerClass("TestTimerJob", site.WebApplication);
-
- SPDailySchedule schedule = new SPDailySchedule();
- schedule.BeginHour = 4;
- schedule.EndHour = 5;
- schedule.BeginMinute = 30;
- schedule.EndMinute = 0;
- schedule.BeginSecond = 0;
- schedule.EndSecond = 0;
- corpProfileJob.Schedule = schedule;
- elevatedSite.WebApplication.JobDefinitions.Add(corpProfileJob);
- }
- });
- } catch (Exception ex) {}
- }
- Add the code, given below, inside FeatureDeactivated Event.
-
- public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
- try {
- SPSite site = properties.Feature.Parent as SPSite;
- SPSecurity.RunWithElevatedPrivileges(delegate() {
- using(SPSite elevatedSite = new SPSite(site.ID)) {
-
- foreach(SPJobDefinition lockjob in elevatedSite.WebApplication.JobDefinitions) {
- if (lockjob.Name == "TestTimerJob") {
- lockjob.Delete();
- }
- }
- }
- });
- } catch (Exception ex) {}
- }
- Make the Activation on Default of the feature as false and always make Force Install as true in Feature properties.
![Always Force Install]()
- Deploy our solution into SharePoint portal.
![Deploy]()
- After completing deployment, activate the feature in Web Application.
![activate the feature]()
- See the timer job, which gets listed on the Monitoring->Review Job Definitions in Central Administration.
s
![timer job]()
- To debug the timer job, we need to attach with the process OWSTIMER.EXE to debug the timer job. On the execution, Execute method will be the first method, which gets invoked.
![debug]()
Summary
Thus, you have learned, how to Create a Timer Job in SharePoint 2013 programmatically, using Server-Side Object Model.