Create A Timer Job In Sharepoint 2013 Programmatically Using Server-Side Object Model

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

  1. Open Visual Studio.
  2. Open New Project dialog box, expand the Office/SharePoint node and choose SharePoint Solutions.

    Office/SharePoint node

  3. Choose SharePoint 2013 – Empty Project template. Name the project TimerProject and click OK button.

    Empty

  4. Choose Deploy as a farm solution option button and then choose Finish button to accept the default local SharePoint site.

    Deploy

  5. In Solution Explorer, choose TimerProject project, we created.

    Solution Explorer

Create Timer Job

  1. Right click on the Project and select New Folder.

    New Folder

  2. Name the folder as TimerJob.

    TimerJob

  3. Add a class file inside TimerJob folder.

    TimerJob folder

  4. The coding for the class file is given below-
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6. namespace TimerProject.TimerJob {  
    7.     class TimerClass {}  
    8. }  
    code

  5. Inherit SPJobDefinition class and make the class as public and create the constructors.
    1. using Microsoft.SharePoint.Administration;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Linq;  
    5. using System.Text;  
    6. using System.Threading.Tasks;  
    7. namespace TimerProject.TimerJob {  
    8.     class TimerClass: SPJobDefinition {  
    9.         public TimerClass(): base() {}  
    10.         public TimerClass(string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType) {}  
    11.         public TimerClass(string jobName, SPWebApplication webApplication): base(jobName, webApplication, null, SPJobLockType.ContentDatabase) {  
    12.             this.Title = jobName;  
    13.         }  
    14.         public override void Execute(Guid contentDbId) {  
    15.             try {} catch (Exception ex) {}  
    16.         }  
    17.     }  
    18. }  
  6. Next, we need to create a Feature and a FeatureReceiver.
  7. Add a feature by right clicking on Features folder.

    Features folder

  8. Name the feature. Make the scope of the Feature as WebApplication.

    WebApplication

  9. Add the event receiver to the Feature by right-clicking on the feature name.

    Add the event

  10. Add the code, given below, inside the FeatureActivated Event.
    1.     // Uncomment the method below to handle the event raised after a feature has been activated.   
    2. public override void FeatureActivated(SPFeatureReceiverProperties properties) {  
    3.     try {  
    4.         SPSite site = properties.Feature.Parent as SPSite;  
    5.         SPSecurity.RunWithElevatedPrivileges(delegate() {  
    6.             using(SPSite elevatedSite = new SPSite(site.ID)) {  
    7.                 // make sure the job isn't already registered   
    8.                 foreach(SPJobDefinition job in elevatedSite.WebApplication.JobDefinitions) {  
    9.                         if (job.Name == "TestTimerJob") {  
    10.                             job.Delete();  
    11.                             break;  
    12.                         }  
    13.                     }  
    14.                     // install the job   
    15.                 TimerClass corpProfileJob = new TimerClass("TestTimerJob", site.WebApplication);  
    16.                 // Updates the timer schedule values   
    17.                 SPDailySchedule schedule = new SPDailySchedule();  
    18.                 schedule.BeginHour = 4;  
    19.                 schedule.EndHour = 5;  
    20.                 schedule.BeginMinute = 30;  
    21.                 schedule.EndMinute = 0;  
    22.                 schedule.BeginSecond = 0;  
    23.                 schedule.EndSecond = 0;  
    24.                 corpProfileJob.Schedule = schedule;  
    25.                 elevatedSite.WebApplication.JobDefinitions.Add(corpProfileJob);  
    26.             }  
    27.         });  
    28.     } catch (Exception ex) {}  
    29. }  
  11. Add the code, given below, inside FeatureDeactivated Event.
    1. // Uncomment the method below to handle the event raised before a feature is deactivated.   
    2. public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {  
    3.     try {  
    4.         SPSite site = properties.Feature.Parent as SPSite;  
    5.         SPSecurity.RunWithElevatedPrivileges(delegate() {  
    6.             using(SPSite elevatedSite = new SPSite(site.ID)) {  
    7.                 // delete the job   
    8.                 foreach(SPJobDefinition lockjob in elevatedSite.WebApplication.JobDefinitions) {  
    9.                     if (lockjob.Name == "TestTimerJob") {  
    10.                         lockjob.Delete();  
    11.                     }  
    12.                 }  
    13.             }  
    14.         });  
    15.     } catch (Exception ex) {}  
    16. }  
  12. Make the Activation on Default of the feature as false and always make Force Install as true in Feature properties.

    Always Force Install

  13. Deploy our solution into SharePoint portal.

    Deploy

  14. After completing deployment, activate the feature in Web Application.

    activate the feature

  15. See the timer job, which gets listed on the Monitoring->Review Job Definitions in Central Administration.
    s
    timer job

  16. 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.