Problem Activating Sharepoint Timer Job
- by Ben Robinson
I have created a very simple sharepoint timer job. All i want it to do is iterate through a list and update each list item so that it triggers an existing workflow that works fine. In other words all i am trying to do is work around the limitation that workflows cannot be triggered on a scheduled basis. I have written a class that inherits from SPJobDefinition that does the work and i have a class that inherits from SPFeatureReceiver to install and activate it. I have created the feature using SPVisualdev that my coleagues have used in the past for other SP development.
My Job class is below:
public class DriverSafetyCheckTrigger : SPJobDefinition
{
private string pi_SiteUrl;
public DriverSafetyCheckTrigger(string SiteURL, SPWebApplication WebApp):base("DriverSafetyCheckTrigger",WebApp,null, SPJobLockType.Job)
{
this.Title = "DriverSafetyCheckTrigger";
pi_SiteUrl = SiteURL;
}
public override void Execute(Guid targetInstanceId)
{
using (SPSite siteCollection = new SPSite(pi_SiteUrl))
{
using (SPWeb site = siteCollection.RootWeb)
{
SPList taskList = site.Lists["Driver Safety Check"];
foreach(SPListItem item in taskList.Items)
{
item.Update();
}
}
}
}
}
And the only thing in the feature reciever class is that i have overridden the FeatureActivated method below:
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 == "DriverSafetyCheckTrigger")
job.Delete();
}
// Install the job.
DriverSafetyCheckTrigger oDriverSafetyCheckTrigger = new DriverSafetyCheckTrigger(site.Url, site.WebApplication);
SPDailySchedule oSchedule = new SPDailySchedule();
oSchedule.BeginHour = 1;
oDriverSafetyCheckTrigger.Schedule = oSchedule;
oDriverSafetyCheckTrigger.Update();
}
The problem i have is that when i try to activate the feature it throws a NullReferenceException on the line oDriverSafetyCheckTrigger.Update(). I am not sure what is null in this case, the example i have followed for this is this tutorial. I am not sure what I am doing wrong.