Steps to Create Timer Job in SharePoint

  1. Steps to Create Timer Job

//Create class derived from SPJonDefinition Class

class ListTimerJob : SPJobDefinition

{

public ListTimerJob()

base()

{

}

public ListTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)

base(jobName, service, server, targetType)

{

}

public ListTimerJob(string jobName, SPWebApplication webApplication)

: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)

{

this.Title = “List Timer Job”;

}

public override void Execute(Guid contentDbId)

{

// get a reference to the current site collection’s content database

SPWebApplication webApplication = this.Parent as SPWebApplication;

SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];

// get a reference to the “ListTimerJob” list in the RootWeb of the first site collection in the content database

SPList Listjob = contentDb.Sites[0].RootWeb.Lists[“ListTimerJob”];

// create a new list Item, set the Title to the current day/time, and update the item

SPListItem newList = Listjob.Items.Add();

newList[“Title”] = DateTime.Now.ToString();

newList.Update();

}

}

//Add Event receiver at Feature Level

[Guid(“9a724fdb-e423-4232-9626-0cffc53fb74b”)]

public class Feature1EventReceiver : SPFeatureReceiver

{

const string List_JOB_NAME = “ListLogger”;

// Uncomment the method below to handle the event raised after a feature has been activated.

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

SPSite site = properties.Feature.Parent as SPSite;

// make sure the job isn’t already registered

foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)

{

if (job.Name == List_JOB_NAME)

job.Delete();

}

// install the job

ListTimerJob listLoggerJob = new ListTimerJob(List_JOB_NAME, site.WebApplication);

SPMinuteSchedule schedule = new SPMinuteSchedule();

schedule.BeginSecond = 0;

schedule.EndSecond = 59;

schedule.Interval = 5;

listLoggerJob.Schedule = schedule;

listLoggerJob.Update();

}

// Uncomment the method below to handle the event raised before a feature is deactivated.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

SPSite site = properties.Feature.Parent as SPSite;

// delete the job

foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)

{

if (job.Name == List_JOB_NAME)

job.Delete();

}

}

  1. b.    Debug Timer Job: OWSTIMER.EXE
  2. OWSTIMER.EXE is the process executing Timer Jobs
  3. d.     View the Results
    1. Now we can go to the Central Administration to see our Timer Job.  For this open Central Administration web site and go to Monitoring > Review Job Definitions

 

  • On clicking the Review Job Definitions link you can see our item as shown below

  • Click on the item and in the appearing page click the button Run Now

 

  • You can see that the Timer Job is set for 5 Minutes according to our code.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s