SurfRay

Timers in MOSS 2007 Part 2 - Using a custom timer job

E-mail Print

This article builds on the example used in the previous article. "Timers in MOSS 2007 Part 1 - Creating a customer timer job"

There are many ways to add a custom timer job. Both programmatically and using stsadm.exe. I however prefer to use a farm feature and the feature receiver.

The feature receiver

In my opinion it is good practice to use the feature receiver (of a farm feature) to add and remove custom jobs. This way the job gets added and removed when the farm administrator activates and deactivates your custom feature. Another option could be to use the FeatureInstalled and FeatureUninstalled methods if so required.

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

 

namespace Custom.SharePoint.Administration

{

public class CustomFeatureReceiver : SPFeatureReceiver

{

protected CustomFeatureReceiver()

{;}

 

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

base.FeatureActivated(properties);

 

SPFarm farm = ((SPPersistedObject)properties.Feature.Parent).Farm;

if (farm != null)

{

SPSecurity.RunWithElevatedPrivileges(delegate { DoActivate(farm); });

}

}

 

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

base.FeatureDeactivating(properties);

 

SPFarm farm = ((SPPersistedObject)properties.Feature.Parent).Farm;

if (farm != null)

{

SPSecurity.RunWithElevatedPrivileges(delegate { DoDeactivate(farm); });

}

}

}

}

 

 

Adding and removing custom jobs require farm administrator credentials. This is ensured by calling the SPSecurity.RunWithElevatedPrivileges method in the feature receiver of a feature scoped for the farm. You should be aware that this will not work for features scoped for web applications or sites.

Adding the custom job on feature activation

Based on the CustomJobDefinition from part 1 of this article, you can create the custom timer job as seen below.

As I have used a fixed name for the custom job in the example from part 1, the method will throw an exception if the job already exists.

You have the option to create a number of different schedules, such as one time schedules, minute schedules monthly schedules and those in between. The schedules themselves are trivial to create.

You append a schedule to the job and the call the Update method on the job. Remember that a SharePoint job is basically a persisted object.

private void DoActivate(SPFarm farm)

{

try

{

CustomJobDefinition job = new CustomJobDefinition(farm.TimerService);

SPHourlySchedule schedule = new SPHourlySchedule();

schedule.BeginMinute = 0;

schedule.EndMinute = 59;

job.Schedule = schedule;

job.Update();

}

catch (Exception ex)

{

//Handle errors

throw;

}

}

 

 

Removing the custom job on feature deactivation

We delete the custom job by first getting the Id of the job and then remove it from the JobDefinitions list. The Name property of the job could also be used but that would require us to iterate all jobs to find it.

private void DoDeactivate(SPFarm farm)

{

try

{

CustomJobDefinition job = new CustomJobDefinition(farm.TimerService);

DeleteTimerJob(farm, job.Id)

}

catch (Exception ex)

{

//Handle errors

throw;

}

}

 

 

 

It should be emphasized that you in your code need to maintain a reference to a local copy of the timer service object. Otherwise you would remove the job from one local instance, get a new instance (without the job removed) and actually updating the unchanged instance.

protected static void DeleteTimerJob(SPFarm farm, Guid jobId)

    {
       SPTimerService timerService = farm.TimerService;
       timerService.JobDefinitions.Remove(jobId);

timerService.Update();

}

 

 

 

 
Get Upgrade
Get Trial
Get Price Quote
Get Support
Get Partnership