banner



How To Create Timer Job In Sharepoint 2013 Programmatically

  • Updated date Sep 06, 2016
  • 36.6k
  • 5

In this article, you will learn, how to 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. public  override void  FeatureActivated(SPFeatureReceiverProperties properties) {
    2. try  {
    3.         SPSite site = properties.Feature.Parent as SPSite;
    4.         SPSecurity.RunWithElevatedPrivileges(delegate() {
    5.             using(SPSite elevatedSite =new  SPSite(site.ID)) {
    6.                 foreach(SPJobDefinition jobin  elevatedSite.WebApplication.JobDefinitions) {
    7. if  (job.Name == "TestTimerJob" ) {
    8.                             job.Delete();
    9. break ;
    10.                         }
    11.                     }
    12.                 TimerClass corpProfileJob =new  TimerClass( "TestTimerJob" , site.WebApplication);
    13.                 SPDailySchedule schedule =new  SPDailySchedule();
    14.                 schedule.BeginHour = 4;
    15.                 schedule.EndHour = 5;
    16.                 schedule.BeginMinute = 30;
    17.                 schedule.EndMinute = 0;
    18.                 schedule.BeginSecond = 0;
    19.                 schedule.EndSecond = 0;
    20.                 corpProfileJob.Schedule = schedule;
    21.                 elevatedSite.WebApplication.JobDefinitions.Add(corpProfileJob);
    22.             }
    23.         });
    24.     }catch  (Exception ex) {}
    25. }
  11. Add the code, given below, inside FeatureDeactivated Event.
    1. public  override void  FeatureDeactivating(SPFeatureReceiverProperties properties) {
    2. try  {
    3.         SPSite site = properties.Feature.Parent as SPSite;
    4.         SPSecurity.RunWithElevatedPrivileges(delegate() {
    5.             using(SPSite elevatedSite =new  SPSite(site.ID)) {
    6.                 foreach(SPJobDefinition lockjobin  elevatedSite.WebApplication.JobDefinitions) {
    7. if  (lockjob.Name == "TestTimerJob" ) {
    8.                         lockjob.Delete();
    9.                     }
    10.                 }
    11.             }
    12.         });
    13.     }catch  (Exception ex) {}
    14. }
  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.

How To Create Timer Job In Sharepoint 2013 Programmatically

Source: https://www.c-sharpcorner.com/article/create-a-timer-job-in-sharepoint-2013-programmatically-using-server-side-object/

Posted by: fitzgeraldforeas.blogspot.com

0 Response to "How To Create Timer Job In Sharepoint 2013 Programmatically"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel