Brian McKeiver's Blog

Registering Custom Classes in Kentico 8 Has Never Been Easier


Since the Kentico 8 Upgrade tool dropped, I have been itching to get some time in on upgrading my Kentico blog here at Mcbeev.com. Tonight I was working on viewing the results of the upgraded code, and I was also reviewing some newer changes in the API when it comes to custom Scheduled Tasks.

In doing my review, I noticed that there is a subtle change to the way that you register custom classess for things like Scheduled Tasks, Search Indexes, and/or Payment Gateways, and it is way easier! Previously in Kentico versions earlier than 8.0, if you wanted to create and use a custom class file you were forced into writing a separate ModuleLoader class. Now you can use a new attribute in one single class in the form of:

[assemblyRegisterCustomClass("CustomTask"typeof(CustomTask))]

And the icing on the top is that now the Admin UI dynamically picks up these custom classes and makes the available to you at run time, as if they were built in classes. As soon as you choose (custom class) in the Form Control of the New Task screen, the next input automatically has your class in it. You no longer have to type in the Namespace.Classname and cross your fingers for when you go to run the Task the first time to see if it works. Kentico 8 does it for you now!

 

Register Custom Class in Kentico 8

 

The full code sample is below for creating a custom Scheduled Task in Kentico 8.0 in case you want to see the updated version.

using CMS;
using CMS.Scheduler;
using CMS.EventLog;
 
/// 
/// Custom Scheduled Task for Kentico 8
/// 
[assemblyRegisterCustomClass("CustomTask"typeof(CustomTask))]
public class CustomTask : ITask
{
    public CustomTask() {}
 
    /// 
    /// Executes the task.
    /// 
    /// "ti">TaskInfo object representing the scheduled task
    public string Execute(TaskInfo ti)
    {
        string detail = "Executed from '/App_Code'. Task data:" + ti.TaskData;
 
        EventLogProvider.LogInformation("CustomTask""Execute", detail);
 
        return null;
    }
}