Creating and updating a SharePoint timer job in C#

  1. First of all create a new empty sharepoint project.
  2. Add a new feature.
  3. Right click on the feature and add an event receiver.
  4. Right click on the project name and add a new c# class.

Your structure should look like:

 

In my case the class file i added was called ComplianceData.cs your will obviously be different.

Creating the timer job
Modify the class you created by extending : SPJobDefinition

Next create the constructors like below for your class:

public ComplianceData()

: base()

{

}

 

 

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

: base(jobName, service, server, targetType)

{

}

 

 

public ComplianceData(string jobName, SPWebApplication webApplication)

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

{

this.Title = “MiWorld Compliance Feed Refresh”;

}

 

After this create an execute method:

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];

 

//do whatever you need here

 

}

 

Activating the timer job

Next open the feature event receiver class and add the below code:

const string List_JOB_NAME = “MyTimerJob”;

 

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

 

// make sure the job isn’t already registered

 

foreach (SPJobDefinition job in webApp.JobDefinitions)

{

 

if (job.Name == List_JOB_NAME)

 

job.Delete();

 

}

 

// install the job

 

ComplianceData listLoggerJob = new ComplianceData(List_JOB_NAME, webApp);

 

SPMinuteSchedule schedule = new SPMinuteSchedule();

 

schedule.BeginSecond = 0;

 

schedule.EndSecond = 59;

 

schedule.Interval = 30;

 

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)

{

SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;

 

// delete the job

 

foreach (SPJobDefinition job in webApp.JobDefinitions)

{

if (job.Name == List_JOB_NAME)

{

job.Delete();

}

}

}

 

In the code above we’re creating a new timer job once the feature is activated and settings its frequency to run every thirty minutes. You can also use SPHourlySchedule or SPDailySchedule. We also tell the timer which class to execute when the timer job runs.

The final thing to do is to set the feature scope to WebApplication.

All done.

Updating the timer job
If you decide to make changes after deploying the solution, you may find your timer job doesnt update. This is because you need to change the assembly versions in the AssemblyInfo.cs file.