---
title: Scheduling custom tasks
related:
  - https://docs.kentico.com/k8/configuring-kentico/scheduling-tasks.md
  - https://docs.kentico.com/k8/custom-development/loading-custom-classes-from-app_code.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

The process of scheduling a custom task includes two steps:

1. [Writing the code](#writing-the-task-code) that performs the required actions
2. [Creating a new scheduled task in the Kentico administration interface](#creating-new-scheduled-tasks)

## Writing the task code

You need to define each scheduled task as a class that implements the **CMS.Scheduler.ITask** interface. To integrate this type of class into the application, you can:

- Create a new assembly (Class library) in your web project and include the task class there. In this case, you must add the appropriate references to both the assembly and the main Kentico project.
- Define the scheduled task in **App\_Code** and load the class via the API. This ensures that the system automatically compiles the task and you do not need to use a separate assembly. The following example uses the App\_Code approach.

> **Note:** **Choosing the correct option for external service**
>
> You cannot define the task in the _App\_Code_ folder if you wish to use the external service. To run a custom task externally, you must add a new assembly to your project and then define the task class there.

### Defining a custom App\_Code class task

1. Open your web project in Visual Studio.
2. Add a new class into the **App\_Code** folder (or **CMSApp\_AppCode -> Old\_App\_Code** if the project is installed as a web application). For example, name the class **CustomTask.cs**.
3. Edit the class and add the following references:

   ```csharp

   using CMS.Scheduler;
   using CMS.EventLog;


   ```
4. (Optional) Wrap the class into the **Custom** namespace.

   ```csharp

   namespace Custom
   {
        /// <summary>
        /// Custom task class.
        /// </summary>
        public class CustomTask
        {

        }
   }

   ```
5. Make the class implement the **ITask** interface.

   ```csharp

   public class CustomTask: ITask

   ```
6. Define the **Execute** method in the class:

   ```csharp

   /// <summary>
   /// Executes the task.
   /// </summary>
   /// <param name="ti">Info object representing the scheduled task</param>
   public string Execute(TaskInfo ti)
   {
       string detail = "Executed from '~/App_Code/CustomTask.cs'. Task data:" + ti.TaskData;

       // Logs the execution of the task in the event log.
       EventLogProvider.LogInformation("CustomTask", "Execute", detail);

       return null;
   }

   ```

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](https://docs.kentico.com/k8/developing-websites/troubleshooting-websites/working-with-the-system-event-log.md) 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.

### Loading the App\_Code class for the task

For tasks defined in the _App\_Code_ folder, you need to ensure that the system loads the appropriate class when executing the scheduled task. You can find additional information related to this topic in [Loading custom classes from App\_Code](https://docs.kentico.com/k8/custom-development/loading-custom-classes-from-app_code.md).

1. Add a reference to the **CMS** namespace to your scheduled task file:

   ```csharp

   using CMS;

   ```
2. Add the **RegisterCustomClass** assembly attribute above the class declaration (outside of the _Custom_ namespace).

   ```csharp

   [assembly: RegisterCustomClass("Custom.CustomTask", typeof(Custom.CustomTask))]

   ```

This attribute registers custom classes and makes them available in the system. The attribute accepts two parameters:

- The first parameter is a _string_ identifier representing the name of the class. The name must match the value of the **Task provider - Class** field specified for the given scheduled task (_Custom.CustomTask_ in this example).
- The second parameter specifies the type of the class as a **System.Type** object. When the system executes the scheduled task, the attribute ensures that an instance of the given class is provided.

## Creating new scheduled tasks

1. Log in to the Kentico administration interface and open the **Scheduled tasks** application.
2. Select the **Site** for which you wish to schedule the task _((global)_ if you want the task to run for all sites or affect global objects)_._
3. Click **New task** and fill in the [properties of the task](https://docs.kentico.com/k8/configuring-kentico/scheduling-tasks/reference-scheduled-task-properties.md).

   ![Registering a new custom task](https://docs.kentico.com/docsassets/k8/scheduling-custom-tasks/New_Custom_Task.png "Registering a new custom task")
4. 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](https://docs.kentico.com/docsassets/k8/scheduling-custom-tasks/Custom_Task_Result.png "Information events logged by the custom scheduled task")
