I got a SharePoint-Feature under the WebApplication-Scope that should create a Timer-Job.
On feature-activation (before creating that Job) and on feature-deaktivation I want to delete the job if it already exists. This is the feature-activation-Code:
public class myJob : SPJobDefinition
public myJob() : base("JobName", SPAdministrationWebApplication.Local, null, SPJobLockType.Job) {}
}
public class JobFeature : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication application = properties.Feature.Parent as SPWebApplication;
// 1. Delete old Job if exists
foreach (SPJobDefinition job in application.JobDefinitions)
{
if (string.Equals(job.Name, "JobName"))
{
job.Delete(); // NEVER GETS HERE
}
}
// 2. Install job
myJob deploymentJob = new myJob();
SPMinuteSchedule schedule = new SPMinuteSchedule { BeginSecond = 0, EndSecond = 59, Interval = 5 };
deploymentJob.Schedule = schedule;
deploymentJob.Update(); // CRASHES
}
}
The Problem is: The code that should delete the job is never reached. The job just does not seem to be in application.JobDefinitions. ("// NEVER GETS HERE" in the code above)
But when I try to create the deploymentjob I got an exception that the job already exists ("// CRASHES" in the code above):
{"An object of the type myJob named \"JobName\" already exists under the parent Microsoft.SharePoint.Administration.SPAdministrationWebApplication named \"\". Rename your object or delete the existing object."}
Where is the error?
When using another constructor it works...
public myJob(SPWebApplication application)
: base("JobName", application, null, SPJobLockType.Job)
{
}