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.