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

So I want to run a timer job only on weekdays. Does anybody have any experience with a similar requirement?

I can think of two options:

  1. Inherit from the SPSchedule class, and write my own custom schedule class.
  2. Use the SPSchedule.FromString() method to read a custom schedule from a string. But I am not sure if I will be able to get exactly what I want from this. The only "documentation" that I have found regarding this function is here

What would be the best way to go about this?

share|improve this question

1 Answer 1

You have to have SPJobDefinition file and the feature file to do this.

 public class ABCTimerJob : SPJobDefinition
    {

        public static string ABCTimerJobName = "ABC timer Job";

        public ABCTimerJob ()

            : base()
        {
            this.Title = TimerJobName ;
        }

        public ABCTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)

            : base(jobName, service, server, targetType)
        {
            this.Title = TimerJobName ;
        }

        public ABCTimerJob(string jobName, SPWebApplication webApplication)

            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {

            this.Title = TimerJobName ;

        }

        public override void Execute(Guid contentDbId)
        {

            if (DateTime.Today.DayOfWeek != DayOfWeek.Saturday && DateTime.Today.DayOfWeek != DayOfWeek.Sunday)
            { 

             //your code here 
            }

        }
}

In feature activation event you have to do the follwing

        ABCTimerJob abcTimerJob = new ABCTimerJob(ABCTimerJob.ABCTimerJobName, site.WebApplication);
        SPDailySchedule schedule = new SPDailySchedule();
        schedule.BeginHour= 1;
        schedule.EndHour = 1;


        abcTimerJobTimerJob.Schedule = schedule;
        abcTimerJobTimerJob.Update();
share|improve this answer

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.