Here's my FeatureActivated
method:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site;
MyCustomJob customJob;
SPDailySchedule schedule;
site = properties.Feature.Parent as SPSite;
customJob = new MyCustomJob(site.WebApplication, site);
schedule = new SPDailySchedule();
schedule.BeginHour = 1;
schedule.BeginMinute = 0;
schedule.EndHour = 1;
schedule.EndMinute = 59;
customJob.Schedule = schedule;
customJob.Update();
}
And here is the MyCustomJob
class:
public class MyCustomJob : SPJobDefinition
{
private SPSite site;
public MyCustomJob()
: base()
{
}
public MyCustomJob(string jobTitle, SPService service, SPServer server, SPJobLockType lockType)
: base(jobTitle, service, server, lockType)
{
this.Title = jobTitle;
}
public MyCustomJob(string jobTitle, SPWebApplication webApplication)
: base(jobTitle, webApplication, null, SPJobLockType.None)
{
this.Title = jobTitle;
}
public MyCustomJob(SPWebApplication webApplication, SPSite site)
: base("MyCustomJob", webApplication, null, SPJobLockType.None)
{
this.Title = "My Custom Job";
this.site = site;
}
public override void Execute(Guid targetInstanceId)
{
SPSite mySite;
// This is always null!!!
mySite = this.site;
}
}
When I debug the solution, I see that every time the timer job needs to be instantiated, it is done using the default constructor. If that is the case, it's no wonder this.site
is always null.
So the question is: Why is the last constructor never called and how can I have the SPSite object available in the timer job?