Kentico CMS 7.0 Developer's Guide

Managing scheduled tasks

Managing scheduled tasks

Previous topic Next topic Mail us feedback on this topic!  

Managing scheduled tasks

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a scheduled task.

 

private void CreateScheduledTask()
{
    // Create new scheduled task object
    TaskInfo newTask = new TaskInfo();
 
    // Set the properties
    newTask.TaskDisplayName = "My new task";
    newTask.TaskName = "MyNewTask";
    newTask.TaskAssemblyName = "CMS.WorkflowEngine";
    newTask.TaskClass = "CMS.WorkflowEngine.ContentPublisher";
 
    // Create interval
    TaskInterval interval = new TaskInterval();
 
    // Set interval properties
    interval.Period = SchedulingHelper.PERIOD_DAY;
    interval.StartTime = DateTime.Now;
    interval.Every = 2;
 
    // Add some days to interval
    ArrayList days = new ArrayList();
    days.Add(DayOfWeek.Monday.ToString());
    days.Add(DayOfWeek.Sunday.ToString());
    days.Add(DayOfWeek.Thursday.ToString());
 
    interval.Days = days;
 
    newTask.TaskInterval = SchedulingHelper.EncodeInterval(interval);
    newTask.TaskSiteID = CMSContext.CurrentSiteID;
    newTask.TaskData = "<data></data>";
    newTask.TaskEnabled = true;

   newTask.TaskNextRunTime = SchedulingHelper.GetNextTime(newTask.TaskInterval, DateTime.Now, DateTime.Now);

 
    // Save the scheduled task
    TaskInfoProvider.SetTaskInfo(newTask);
}

 

The following example gets and updates a scheduled task.

 

private bool GetAndUpdateScheduledTask()
{

  // Get the scheduled task
  TaskInfo updateTask = TaskInfoProvider.GetTaskInfo("MyNewTask", CMSContext.CurrentSiteID);

    if (updateTask != null)
    {
        // Update the properties
        updateTask.TaskDisplayName = updateTask.TaskDisplayName.ToLower();
 
        // Save the changes
        TaskInfoProvider.SetTaskInfo(updateTask);
 
        return true;
    }
 
    return false;
}

 

The following example gets and bulk updates scheduled tasks.

 

private bool GetAndBulkUpdateScheduledTasks()
{
    // Get the data
    DataSet tasks = TaskInfoProvider.GetAllTasks();
    if (!DataHelper.DataSourceIsEmpty(tasks))
    {
        // Loop through the individual items
        foreach (DataRow taskDr in tasks.Tables[0].Rows)
        {
            // Create object from DataRow
            TaskInfo modifyTask = new TaskInfo(taskDr);
 
            // Update the properties
            modifyTask.TaskDisplayName = modifyTask.TaskDisplayName.ToUpper();
 
            // Save the changes
            TaskInfoProvider.SetTaskInfo(modifyTask);
        }
 
        return true;
    }
 
    return false;
}

 

The following example deletes a scheduled task.

 

private bool DeleteScheduledTask()
{

  // Get the scheduled task
  TaskInfo deleteTask = TaskInfoProvider.GetTaskInfo("MyNewTask",CMSContext.CurrentSiteID);

 
    // Delete the scheduled task
    TaskInfoProvider.DeleteTaskInfo(deleteTask);
 
    return (deleteTask != null);
}

 

The following example runs a scheduled task.

 

private bool RunTask()
{

  // Get the scheduled task
  TaskInfo runTask = TaskInfoProvider.GetTaskInfo("MyNewTask", CMSContext.CurrentSiteID);

 
    if (runTask != null)
    {
        // Run task
        SchedulingExecutor.ExecuteTask(runTask);
 
        return true;
    }
 
    return false;
}