Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

My goal is obviously to exceute some code periodically through SharePoint and I cant get it to work correctly. I started off by creating an empty sharepoint 2010 project(farm solution). Then, I added a feature and a feature reciever and I uncommented the methods that I needed in the feature reciever. The code for my feature is below;

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWebApplication webApplication = (SPWebApplication)properties.Feature.Parent;
        DeleteJob(webApplication);
        AccrualJob job = new AccrualJob("AccrualJob", webApplication);
        SPMinuteSchedule schedule = new SPMinuteSchedule();
        schedule.BeginSecond = 0;
        schedule.EndSecond = 59;
        schedule.Interval = 5;
        job.Schedule = schedule;
        job.Update();
    }


    // Uncomment the method below to handle the event raised before a feature is      deactivated.

    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
    {
        SPWebApplication webApplication = (SPWebApplication)properties.Feature.Parent;
        DeleteJob(webApplication);
    }

    private void DeleteJob(SPWebApplication webApplication)
    {
        foreach (SPJobDefinition job in webApplication.JobDefinitions)
        {
            if (job.Name == "AccrualJob") {
                job.Delete();
                break;
            }
        }
    }

The code is exactly what I have seen over and over again on google. The code for my Timer job class is below;

public class AccrualJob : SPJobDefinition
    {
        public AccrualJob() : base() { }
        public AccrualJob(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.Job)
        {
            this.Title = jobName;
        }
        public override void Execute(Guid targetInstanceId)
        {
            using (SPSite site = new SPSite("http://whatever"))
            {
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists["AccrualList"];
                SPListItem item = list.Items.Add();
                item["Title"] = "MyTitle";
                item["AccrualType"] = "CreditCard";
                item.Update();
            }



        }
    }

When I deploy and look any central admin, the code executes every 5 minutes and it succeeds. However it doesnt execute the code in the override Execute method. Any ideas would be greatly appreciated.

share|improve this question

1 Answer 1

Try to debug your timer job by attaching the debugger to the OWSTIMER.exe process.

Make sure whenever you update your custom timer job class and update the WSP, you also need to restart the timer job service.

share|improve this answer
    
+1 for the "Restart the OWSTimer service". Failing to restart that and wondering why your code doesn't seem to run is a classic. –  Andy Burns Jul 12 '12 at 8:35
    
Falak, thanks a bunch for your help. After restarting the OWSTimer service, my list is being populated periodically. –  user9427 Jul 12 '12 at 15:31
    
Great to know that, you could mark this reply as an answer, if it has has helped you. Cheers –  Falak Mahmood Jul 13 '12 at 7:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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