简体   繁体   中英

How to run a Custom Sharepoint Timer Job on alternate day?

I have to perform some action using Custom Timer Job on alternate day? Code to Create and start Timer job(in feature receiver) below,but it runs daily and i have to run it on alternate days ?

How to do it?

private bool CreateJob(SPSite site) { bool jobCreated = false; try {

            TimerJob job = new TimerJob(JobName, site.WebApplication);
            job.Title = JobName;
            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 15;
            job.Schedule = schedule;

            job.Update();
        }
        catch (Exception)
        {
            return jobCreated;
        }
        return jobCreated;
    }

I think the code above run every 15 minute , you have SPMinuteSchedule , you must use SPDailySchedule . See msdn documentation

However when Job run you can get the current DayOfWeek and run your code when you want

        switch (DateTime.Now.DayOfWeek)
        {
            case DayOfWeek.Friday:
                break;
            case DayOfWeek.Monday:
                break;
            case DayOfWeek.Saturday:
                break;
            case DayOfWeek.Sunday:
                break;
            case DayOfWeek.Thursday:
                break;
            case DayOfWeek.Tuesday:
                break;
            case DayOfWeek.Wednesday:
                break;
            default:
                break;
        }

Or you can save inside Job Property Bag the last run. At this link you can find an example for SPWeb but is the same for SPJob.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM