This class is very handy when it comes to creating a SPSchedule. The question if of course, when do you need to have a SPSchedule? Well it could be very handy for instance when creating application pages that handle the scheduling of custom timer jobs. So instead of creating your own dropdown’s with all the days of the week and all the possible times and converting to those the proper schedule types that SharePoint has (SPMinuteSchedule, SPHourlySchedule, etc) you can use a control that does everything for you. The only thing you need to specify what the options are that an user can select from by setting the properties.
So using this line in my application page:
<%@ Register TagPrefix="wssuc" TagName="SchedulePicker" src="~/_controltemplates/SchedulePicker.ascx" %>
<
wssuc:SchedulePicker
id
="SchedulerAction"
Weekly
="True"
Monthly
="True"
Enabled
="True"
EnableStateView
="True"
runat
="server"
/>
Generates this result:
Pretty cool eh? So if you only want to have the options to schedule during the week you remove the “Monthly=’True’” property and that part of the control will not be visible.
The only code you have to write to schedule your timer job based on what you configure in this control is this:
//reference the control like this
protected SchedulePicker Scheduler
privatevoid InstallTimerJob()
{
CustomTimerJob customTimerJob = new CustomTimerJob("MyCustomJob", webApplication);
customTimerJob.Schedule = Scheduler.Schedule;
customTimerJob.Update();
}
And the code to set the control with the configured values is like this:
protected
override
void OnLoadComplete(EventArgs e)
{
SPWebApplication webApplication = webApplicationSelector.CurrentItem;
if (!Page.IsPostBack)
{
foreach (SPJobDefinition job in webApplication.JobDefinitions)
{
if (job.Name == "MyCustomJob" )
{
Scheduler.ScheduleString = job.Schedule.ToString();
}
}
}
}