Kentico Xperience 13 documentation and ASP.NET Core

Most documentation about running Xperience applications under ASP.NET Core can be found in a dedicated section: Developing Xperience applications using ASP.NET Core. The rest of the documentation still applies, but some code samples and scenarios might need slight modifications for Core projects.

All major differences between the MVC 5 and Core platforms are summarized in Migrating to ASP.NET Core.

×

Scheduling custom tasks

The process of scheduling a custom task includes two steps:

  1. Writing the code that performs the required actions
  2. Creating a new scheduled task in the Xperience administration interface

Writing the task code

You need to define each scheduled task as a class that implements the CMS.Scheduler.ITask interface.

Add the class as part of a custom assembly (Class Library project), and include the assembly into the appropriate solution. Depending on where you plan to execute the task, add the assembly to the Xperience administration application, the live site application, or both.

Preparing a custom assembly

Prepare a separate project for custom classes in your Xperience solution (or use an existing one):

  1. Open your Xperience solution in Visual Studio.
  2. Create a new Class Library project in the solution.
    • The assembly in the example is named Custom, but you can use any other name (e.g. with a unique company prefix).
  3. Add the Xperience API libraries to the project:
    1. Right-click the solution in the Solution Explorer and select Manage NuGet Packages for Solution.
    2. Select the Kentico.Xperience.Libraries package.
    3. Install the package into the project (the version must match your MVC project's Kentico.Xperience.AspNet.Mvc5 package and the exact hotfix version of your Xperience administration project).
  4. Reference the project from the main web application (live site or administration).
On this page

Related pages

Defining the scheduled task class

  1. Create a new class under your custom project. For example, name the class CustomTask.cs.
  2. Edit the class and add using statements for the following namespaces:

    using CMS.Core;
    using CMS.Scheduler;
    
  3. Make the class implement the ITask interface.

    public class CustomTask: ITask
  4. Define the Execute method in the class:

    public class CustomTask : ITask
    {
        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <param name="ti">Info object representing the scheduled task</param>
        public string Execute(TaskInfo ti)
        {
            string details = "Custom scheduled task executed. Task data: " + ti.TaskData;
    
            // Logs the execution of the task in the event log
            Service.Resolve<IEventLogService>().LogInformation("CustomTask", "Execute", details);
    
            // Returns a null value to indicate that the task executed successfully
            // Return an error message string with details in cases where the execution fails
            return null;
        }
    }
  5. Save all changes and Rebuild your solution.

You must always include the Execute method when writing scheduled tasks. The system calls this method whenever the given task is executed, so it needs to contain all code implementing the required functionality.

In this example, the task only creates a record in the application's event log so that you can confirm it is being executed:

  • The TaskInfo parameter of the method allows you to access the data fields of the corresponding scheduled task object. The sample code adds the content of the TaskData field into the details of the event log entry.
  • The string returned by the method is displayed in the administration interface as the result of the task's most recent execution. You can leave it as null in this case.

Creating new scheduled tasks

  1. Sign in to the Xperience administration interface.
  2. Open the Scheduled tasks application.
  3. Select the Site for which you wish to schedule the task (or (global) if you want the task to run for all sites or affect global objects).
  4. Click New task and fill in the properties of the task.
    Registering a new custom task
  5. Click Save.

The system now executes the task regularly according to the specified interval.

Result

To check the result of this sample custom task, open the Event log application and look for entries with CustomTask as the Source.

Information events logged by the custom scheduled task


Was this page helpful?