Ektron CMS400.Net Reference

>>Event Handlers

Chapter 26 - Customizing CMS Behavior with Extensions

CMS Extensions are developer-defined software modules that modify the behavior of the CMS.

In versions previous to 8.0, developers used the Plug-in Extension Wizard to extend the system. As of version 8.0, CMS Extensions are preferred over the Plug-in system.

The benefits of CMS Extensions include the following.

You have full access to the Web application context, the HTTP context, the session information, data stored in cache, and the APIs.

- API access means more functionality is available, such as the Notification system.

Easy to debug because you can attach and set breakpoints in the same way you debug Web applications.

Enhanced performance because no Web Services are required.

No Windows Services are required, because CMS extensions run in the context of your Web application.

This section also contains these topics.

Customizing CMS Behavior with Extensions

Using Visual Studio to Create a New CMS Extension

Register the New Extension in the ObjectFactory

More Information about ObjectFactory objectStrategies

Test the New Extension

More Examples of CMS Extensions

Video Tutorial about CMS Extensions

Event Handlers

An event is a type of activity that occurs in the CMS and is exposed through the CMS Extension Framework. An event handler is a method that executes when an event occurs.

Using Visual Studio to Create a New CMS Extension

Follow these steps to create a new CMS Extension in Visual Studio.

1. In Visual Studio, choose File > New > Project...

2. The New Project screen appears.

3. Under Project Type, choose either Visual Basic or Visual C#. (This example uses Visual C#.)

4. Under Templates, click Class Library.

5. Fill out the Name, Location and Solution Name text boxes.

6. Click OK.

7. The Class1.cs code page appears in the editor.

8. Add references to the following assemblies.

Ektron.CMS.Common
Ektron.CMS.ObjectFactory

You will find these in your <siteroot>/bin directory if they are not already installed in your Visual Studio.

9. Add the following using statements to the top of the class.

using Ektron.Cms;
using Ektron.Cms.Common;
using Ektron.Cms.Extensibility;
using Ektron.Cms.Extensibility.Content;        

10. Update the class to extend Ektron.Cms.Extensibility.ContentStrategy.

namespace Cms.Extensions.Samples
    {
    public class ContentExtensionsSample : ContentStrategy
        {
        }
    }

Note: The namespace in the code above is Cms.Extensions.Samples. This information is needed for step 12 below.

1. In Visual Studio, open your Web site.

2. Add a new C# class to the App_Code directory.

3. The Class1.cs code page appears in the editor.

4. Add references to the following assemblies if they are not already added.

Ektron.CMS.Common
Ektron.CMS.ObjectFactory

If the assemblies are not installed in Visual Studio, you can find them in the siteroot/bin directory.

5. Add the following using statements to the top of the class.

using Ektron.Cms;
using Ektron.Cms.Common;
using Ektron.Cms.Extensibility;
using Ektron.Cms.Extensibility.Content;        

6. Update the class to extend Ektron.Cms.Extensibility.ContentStrategy.

namespace Cms.Extensions.Samples
    {
    public class ContentExtensionsSample : ContentStrategy
        {
        }
    }

Note: The namespace in the code above is Cms.Extensions.Samples. This information is needed for step 8 below.

7. Override the method for the event you wish to catch. In this example, override OnBeforeAddContent(). The code to modify the Title looks like this.

public override void OnBeforeAddContent(ContentData contentData, CmsEventArgs eventArgs)
{
    contentData.Title += " modified";
}        

8. This is what the final code looks like.

using System;
using System.Collections.Generic;
using System.Text;
using Ektron.Cms;
using Ektron.Cms.Common;
using Ektron.Cms.Extensibility;
using Ektron.Cms.Extensibility.Content;
namespace Cms.Extensions.Samples
{
public class ContentExtensionsSample : ContentStrategy
    {
    public override void OnBeforeAddContent(ContentData contentData, CmsEventArgs eventArgs)
        {
        contentData.Title += " modifed";
        }
    }
}

9. Compile this assembly.

10. Copy the resulting dll into the <webroot>\bin directory.

Register the New Extension in the ObjectFactory

After creating the extension in the steps above, register it in the <webroot>/ObjectFactory.config file.

This file already contains GoogleGeoCoder objectStrategies, so you need to add a new Name in the Content area.

In this example, the code looks like this.

<objectFactory>
    <objectStrategies>
        <add name="Content">
            <strategies>
                <add name="MyFirstExample" type="Cms.Extensions.Samples.ContentExtensionsSample, 
ContentExtensionsSample"/>
                <add name="GoogleGeoCoder" type="Cms.Extensions.GoogleGeoCoder.ContentStrategy, 
Cms.Extensions.GoogleGeoCoder"/>
            </strategies>
        </add>
        <add name="User">
            <strategies>
                <add name="GoogleGeoCoder" type="Cms.Extensions.GoogleGeoCoder.UserStrategy, 
Cms.Extensions.GoogleGeoCoder"/>
            </strategies>
        </add>
    </objectStrategies>
</objectFactory> 

More Information about ObjectFactory objectStrategies

The CMS Extension must be registered in the objectStrategies section of the site root/ObjectFactory.config file.

The required node <add name="Content"> indicates we are defining "Content" extensions.

In the <strategies> section, you may add one or more extensions for content.

To add a strategy, you must define two properties: Name and Type.

- Name is any unique name that identifies the extension. A list of valid names is in the table below.

- Type is a fully-qualified name of the type you register as the extension. It contains type name (the NameSpace + type) **and the assembly name of the type.

List of Valid Names

Inventory

Coupon

BasketCalculator

CouponCalculator

ShippingCalculator

TaxCalculator

Country

Region

Basket

Blog

Order

Customer

CatalogEntry

CommunityGroup

Tag

MessageBoard

MicroMessage

Content

Folder

User

Forum

WebEvent

Taxonomy

Test the New Extension

The new CMS extension (created in the examples above) adds "modified" to the end of a new content item's title. To test this, create new content in the workarea. "modified" will be appended to the end of the title.

More Examples of CMS Extensions

More examples of CMS Extensions are found in the SDK folder of the CMS. The default location for this folder is : Program Files\Ektron\CMS400SDK\CMS Extensions.
Also, samples of eCommerce extensions are found in Program Files\Ektron\CMS400SDK\Commerce\Events.

Video Tutorial about CMS Extensions

To see more code samples and a video tutorial on CMS Extensions, visit the Ektron Developer Center at this address: http://dev.ektron.com/blogs.aspx?id=29648&blogid=68.


Visit the Ektron Dev Center at http://dev.ektron.com 1-866 - 4 - EKTRON

Ektron CMS400.NET Reference Version 8.02 SP1 Rev 2

Ektron Documentation,© 2011 Ektron, Inc.