0

I am creating timer job in which the list item is added every 5 minutes.

My class file which inherits SPJobDefinition is :

public class MyTimerJob : SPJobDefinition
{
    public MyTimerJob() : base() { }

    public MyTimerJob(SPWebApplication webApp)
            : base("MyTimerJob", webApp, null, SPJobLockType.None)
    {
        this.Title = "MyCustomTimerJob";
    }
    public override void Execute(Guid targetInstanceId)
    {
        SPSite site = new SPSite("http://bipc11:1234/sites/publishing1");
        SPWeb web = site.RootWeb;
        SPList list = web.Lists["Sample Tasks"];
        SPListItem item = list.Items.Add();
        item["Title"] = "Added Programmatically";
        item.Update();
    }

}

Feature Event Receiver is as below:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSite site = new SPSite("http://bipc11:1234/sites/publishing1");
    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
    {
        if(job.Name.Equals("MyTimerJob"))
            job.Delete();


        MyTimerJob timerJob = new MyTimerJob(site.WebApplication);
        SPMinuteSchedule schedule = new SPMinuteSchedule();
        schedule.BeginSecond = 0;
        schedule.Interval = 5;
        timerJob.Schedule = schedule;
        timerJob.Update();
    }
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{   
      SPSite site = new SPSite("http://bipc11:1234/sites/publishing1");
      foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
      {
          if (job.Name.Equals("MyTimerJob"))
              job.Delete();
      }
}

I the scope of the feature is Site Collection level.

The code is built successfully but when I deploy the solution I am getting the error :

Error 1 Error occurred in deployment step 'Activate Features': An object of the type TimerJob10092013.MyTimerJob named "MyTimerJob" already exists under the parent Microsoft.SharePoint.Administration.SPWebApplication named "SharePoint - 1234". Rename your object or delete the existing object.

I uninstalled the feature from power shell but still I am getting the error?

What am I missing ? Please help.

3 Answers 3

1

This might be it

foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
    if(job.Name.Equals("MyTimerJob"))
        job.Delete();


    MyTimerJob timerJob = new MyTimerJob(site.WebApplication);
    SPMinuteSchedule schedule = new SPMinuteSchedule();
    schedule.BeginSecond = 0;
    schedule.Interval = 5;
    timerJob.Schedule = schedule;
    timerJob.Update();
}

should be

foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
{
    if(job.Name.Equals("MyTimerJob"))
        job.Delete();
}

MyTimerJob timerJob = new MyTimerJob(site.WebApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.Interval = 5;
timerJob.Schedule = schedule;
timerJob.Update();

you're trying to add the exact same timer for every job definition in the web app ;)

1
  • Aaah, silly mistake.. Thank you very much sir, it worked perfectly..
    – users1100
    Sep 10, 2013 at 12:36
0

In looking at my code, compared to yours, the biggest difference i see is that the this.Title in the timer is different from the title in the declaration. Consider doing the following instead

public MyTimerJob(string jobName, SPWebApplication webApp)
            : base(jobName, webApp, null, SPJobLockType.None)
{
    this.Title = jobName;
}

and then in the feature

MyTimerJob timerJob = new MyTimerJob("MyTimerJob", site.WebApplication);

I find this design helps guarantee the naming will be the same each time.

4
  • Thanks for your response sir, I made the changes as per your suggestion but still I am getting same error.
    – users1100
    Sep 10, 2013 at 12:00
  • My bad, didn't see this before... code doens't fly in this comment, will make another answer Sep 10, 2013 at 12:20
  • I did some steps to make sure that there is no solution available. First run powershell command i.e. Get-SPFeature > c:\features.txt. Then, found the feature that was available in the list, got GUID and run the below command, i.e. Uninstall-SPFeature -Identity bce8401c-e22c-43d8-89 f6-3ec816d32bc8 -Force -Confirm:$false again searched for the feature but now that was not available. And after giving deploy command from VS the same error occurred..
    – users1100
    Sep 10, 2013 at 12:28
  • See my other answer above? Sep 10, 2013 at 12:35
0

Try to list out all the timerjobs on the webapp (via PowerShell), if the timerjob is still installed: Uninstall it, and delete it. (from PS)

http://sharepointhangout.com/powershell-script-to-list-timer-jobs-sharepoint-webapplication-get-sptimerjob/

Then try to activate your feature again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.