在SharePoint 2010中的Web应用程序功能中查找作业定义时遇到的问题

时间:2013-01-15 16:22:21

标签: sharepoint sharepoint-2010 jobs schedule

我在WebApplication-Scope下有一个应该创建Timer-Job的SharePoint功能。

在功能激活(创建该作业之前)和功能延迟时,我想删除该作业(如果它已存在)。这是功能激活代码:

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
  }
}

问题是:永远不会到达应该删除作业的代码。这项工作似乎没有在application.JobDefinitions中。 (“//从未在上面的代码中获取”)

但是当我尝试创建deploymentjob时,我得到一个例外,即作业已经存在(上面代码中的“// CRASHES”):

{"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."}

错误在哪里?

1 个答案:

答案 0 :(得分:0)

当使用其他构造函数时,它可以工作......

public myJob(SPWebApplication application)
            : base("JobName", application, null, SPJobLockType.Job)
        {

        }