Using the SchedulePicker
There isn’t much to find about the SchedulePicker Class (in the Microsoft.SharePoint.WebControls namespace) which 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
private void 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();
}
}
}
}
I really love the reusing of SharePoint controls like this! Saves you a lot of effort and time ;)
Technorati Tags:
SharePoint,
Code2 Comments |
Posted
Sunday, 18 Jan 2009 12:50
by
Robin Meure
in
How-to