How can we set timer job so that it will run on specific date of month?
For example, i want schedule timer job to run on 2nd and 28th day of month.
How can we do that?
2 Answers
I'm not sure if it's possible to configure a timer job in such a way to only fire on given dates, however, you could configure your timer job to run daily but include a date check as the first action in the job.
DateTime date = DateTime.Now;
if (date.Day.Equals(2) || date.Day.Equals(28))
{
// It's either the 2nd or 28th of the month!
// Put the rest of the code you want to run in here.
}
else
{
// Do nothing - it's not the 2nd or 28th.
}
Here is a good walk through if you need more information on creating the timer job, taken from MSDN.
[Edit]
In reference to your comments on this answer. See this article from the sub heading Scheduling a Job to Run Each Day.
-
Hi scmc, your answer is very helpful and i really appreciate your help. If i need to configure my timer job on daily basis, should i need to set its interval to seconds(24hours)?– AshishMay 25, 2015 at 14:43
-
Does it need to run just just once on the 2nd/28th? I.e. it does not recur throughout the whole day.– Stuart CMay 25, 2015 at 14:46
-
-
I've editted my answer to include a link on how to schedule a timer job to run once per day.– Stuart CMay 25, 2015 at 14:52
There is no way to schedule timerjob by date. You should create daily timerjob and check if today is the 2nd or 28th day.
-
Thanks, and it will be very helpful if you can give me an example on how to schedule timer job on daily basis through coding.– AshishMay 25, 2015 at 14:28
-
1
YourTimerJob tJob = new YourTimerJob(jobName, webApplication); tJob.Schedule = new SPDailySchedule(){ BeginHour = 4, EndHour = 5, };
And in the Execute method check:if (DateTime.Now.Day.Equals(2) || DateTime.Now.Day.Equals(28)) {your code here}
May 25, 2015 at 14:51