Customizing providers from the App_Code folder

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

Custom providers (e.g. scheduled tasks) can be defined in App_Code, without the need to be compiled into an assembly. This can be achieved by defining a new class implementing the ITask interface in App_Code/Global/CMS/CMSCustom.cs and handle it in the GetCustomClass method.

 

You can find an example of a very simple MyTask class in CMSCustom.cs. The code of the task is displayed below.

 

[C#]

 

/// <summary>

/// Gets the custom class object based on the given class name. This handler is called when the assembly name is App_Code

/// </summary>

/// <param name="className">Class name</param>

public static object GetCustomClass(string className)

{

  // Provide your custom classes

  switch (className)

   {

      // Define the class MyTask implementing ITask and you can provide your scheduled tasks out of App_Code

      case "Custom.MyTask":

          return new MyTask();

   }

 

  return null;

}

 

/// <summary>

/// Sample task class

/// </summary>

public class MyTask : ITask

{

  /// <summary>

  /// Executes the task

  /// </summary>

  /// <param name="ti">Task info</param>

  public string Execute(TaskInfo ti)

   {

       EventLogProvider ev = new EventLogProvider();

       ev.LogEvent("I", DateTime.Now, "MyTask", "Execute", null, "This task was executed from '~/App_Code/Global/CMS/CMSCustom.cs'.");

 

      return null;

   }

}

 

To register the task in Kentico CMS, you need to create a new task in Site Manager -> Administration -> Scheduled tasks.

 

To ensure the required functionality, App_Code must be entered into the Task assembly name field and the class name handled in the GetCustomClass method needs to be entered into the Task class name field. With this task created, the code in the MyTask class is executed repeatedly after the set interval.

 

devguide_clip1034

 

Page url: http://devnet.kentico.com/docs/devguide/index.html?customizing_providers_from_app_code_folder.htm