Kentico CMS 7.0 Developer's Guide

Registering custom classes in App_Code

Registering custom classes in App_Code

Previous topic Next topic Mail us feedback on this topic!  

Registering custom classes in App_Code

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

By preparing custom classes that inherit from an appropriate base class, you can extend the functionality of Kentico CMS. This approach allows you to implement the following types of objects:

 

integration connectors

marketing automation actions

notification gateways

scheduled tasks

smart search custom indexes and analyzers

translation services

workflow actions

 

If you create the custom classes in your project's App_Code folder (or Old_App_Code on web application installations), you do not need to integrate a new assembly into the web project. Code in the App_Code folder is compiled dynamically and automatically referenced in all other parts of the application.

 

Registering custom classes in the App_Code folder

 

To ensure that the system can load custom classes placed in App_Code, you need to write code that registers each class.

 

1. Extend the CMSModuleLoader partial class. You can either extend an existing definition of the class, or create a new source file with the following code.

 

[C#]

 

using System;

 
using CMS.SettingsProvider;

 
public partial class CMSModuleLoader
{

 
}

 

2. Create a new class inside the CMSModuleLoader that inherits from CMSLoaderAttribute.

 

[C#]

 

private class ClassLoader : CMSLoaderAttribute
{

 
}

 

3. Add the attribute defined by the internal class before the definition of the CMSModuleLoader partial class.

 

[C#]

 

[ClassLoader]

public partial class CMSModuleLoader
{
...

 

4. Override the Init method inside the attribute class, and assign a handler to the ClassHelper.OnGetCustomClass event.

 

[C#]

 

public override void Init()
{
  ClassHelper.OnGetCustomClass += GetCustomClassEventHandler;
}

 

5. Define the handler method for the OnGetCustomClass event as follows:

 

[C#]

 

private static void GetCustomClassEventHandler(object sender, ClassEventArgs e)
{
  if (e.Object == null)
   {
      switch (e.ClassName)
       {
          case "CustomAction":
               e.Object = new CustomAction();
              break;
       }
   }
}

 

To load your App_Code class into the system, create a new instance and assign it to the Object property of the event handler's ClassEventArgs parameter (e). The switch structure allows you to use the handler to load any number of custom classes based on the specified class name.

 

Overall code example:

 

[C#]

 

using System;
 
using CMS.SettingsProvider;
 
[ClassLoader]
public partial class CMSModuleLoader
{
   /// <summary>
   /// Attribute class that ensures the loading of custom classes.
   /// </summary>
  private class ClassLoader : CMSLoaderAttribute
   {
      /// <summary>
      /// Called automatically when the application starts.
      /// </summary>
      public override void Init()
       {
          // Assigns a handler for the OnGetCustomClass event
          // This event is triggered when the system requests a class with App_Code as the assembly name
          ClassHelper.OnGetCustomClass += GetCustomClassEventHandler;
       }
 
      /// <summary>
      /// Gets the custom class object based on the specified class name.
      /// </summary>
      private static void GetCustomClassEventHandler(object sender, ClassEventArgs e)
       {
          // Checks whether an object is already assigned to the requested class
          if (e.Object == null)
           {
              // Checks the name of the requested class
              switch (e.ClassName)
               {
                  // Loads an instance of the 'CustomTask' class for the 'CustomTaskName' name
                  case "CustomTaskName":
                       e.Object = new CustomTask();
                      break;
 
                   // Loads an instance of the 'CustomIndex' class for the 'CustomIndexName' name
                  case "CustomIndexName":
                       e.Object = new CustomIndex();
                      break;
               }
           }
       }
   }
}

 

Once you have registered your custom classes, you can use them as the source for objects in the system. When assigning App_Code classes to objects in the editing interface, fill in the following values:

 

Assembly name: App_Code

Class name: must match the name that identifies the corresponding custom class in the switch statement of your OnGetCustomClass handler method. When the OnGetCustomClass event is triggered, the value of the Class name field is passed on through the ClassName property of the ClassEventArgs parameter.

 

Assigning a custom App_Code class to a new scheduled task

Assigning a custom App_Code class to a new scheduled task