My custom sharepoint 2010 Timer job not running

3.4k Views Asked by At

I have created a custom timer service to create a list. its scheduled in minute using FeatureActivated event. I could able to globally deploy the solution. But the my timer job is not running. Its showing last run time N/A. Is there any solution?

My Execute method

    public override void Execute(Guid targetInstanceId)
    {
        ClientContext clientContext = new ClientContext("http://mymachine:0909");
        Web site = clientContext.Web;

        ListCreationInformation listCreationInfo = new ListCreationInformation();
        listCreationInfo.Title = "Test Mailer List";
        listCreationInfo.TemplateType = (int)ListTemplateType.GenericList;
        List list = site.Lists.Add(listCreationInfo);

        Field field1 = list.Fields.AddFieldAsXml(
                  @"<Field Type='Choice'
                  DisplayName='Category'
                  Format='Dropdown'>
             <Default>Specification</Default>
             <CHOICES>
               <CHOICE>Specification</CHOICE>
               <CHOICE>Development</CHOICE>
               <CHOICE>Test</CHOICE>
               <CHOICE>Documentation</CHOICE>
             </CHOICES>
           </Field>", true, AddFieldOptions.DefaultValue);
        Field field2 = list.Fields.AddFieldAsXml(
            @"<Field Type='Number'
                  DisplayName='Estimate'/>", true, AddFieldOptions.DefaultValue);
        clientContext.ExecuteQuery();
    }

And Event receiver class

public class Feature1EventReceiver : SPFeatureReceiver
{


    public const string jobName = "BdayTimer";
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
        DeleteJob(webApp.JobDefinitions);
        TimerJobTest myJob = new TimerJobTest(webApp);

        SPMinuteSchedule schedule = new SPMinuteSchedule();
        schedule.BeginSecond = 0;
        schedule.EndSecond = 59;
        schedule.Interval = 1;

        myJob.Schedule = schedule;
        myJob.Update();

    }


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

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

    public void DeleteJob(SPJobDefinitionCollection job1)
    {
        foreach (SPJobDefinition job in job1)
        {
            if (job.Name.Equals(jobName))
                job.Delete();
        }
    }
2

There are 2 best solutions below

0
On

Reset the Central Administration Pool on the Internet Information Services. It seems to be that the job got stuck. With the pool reset, everything starts working again.

4
On

When you deploy a SharePoint project, you should always restart your sptimer process. Try going into the command prompt (with administrator access) and type:

net stop sptimerv4

then

net start sptimerv4

Your code should work fine now.