Johan Björnfot
Feb 25, 2011
  4907
(0 votes)

New events in DynamicDataStore

In CMS6 R2 we have added some new events to the DynamicDataStore class. Now there are three methods to attach event handlers that are called when Save, Delete or DeleteAll is called on a store instance:

  • public static void RegisterItemSavedEventHandler(string storeName, EventHandler<ItemEventArgs> eventHandler)
  • public static void RegisterItemDeletedEventHandler(string storeName, EventHandler<ItemEventArgs> eventHandler)
  • public static void RegisterDeletedAllEventHandler(string storeName, EventHandler<ItemEventArgs> eventHandler)

There are also corresponding UnRegister methods. The reason that the events do not have the “ordinary” event signature (meaning you cant use += and –= to attach/detach an event handler), is to make the event subscription to be on a per store basis.

New events in DynamicDataSerializer

We have also added two events to the DynamicDataSerializer class:

  • public event EventHandler<SerializationEventArgs> DeserializedObject
  • public event EventHandler<SerializationEventArgs> SerializingObject

The DynamicDataSerializer is used when importing/exporting DDS objects. The typical use case for using the events is if there is an object that has some site specific data that needs to be changed during import or export (e.g. if your object holds a reference to a page).

The SerializingObject event is raised when a DDS object is about to be serialized to a stream (typically used at export). Through an event handler you can either change some state of the object or cancel the object from being written to the stream. The DeserializedObject event is raised when an object has been instantiated from an import stream but not yet saved to the DDS.

To attach to the event during CMS Import, Export, Mirroring or Copy, you do not attach to the event directly on the DynamicDataSerializer but through the DynamicDataTransferHandler on DataExporter or DataImporter like:

    [InitializableModule]
    [ModuleDependency(typeof(DynamicDataTransferHandler))]
    public class MyDDSImportHandler : IInitializableModule
    {
        public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
            DataImporter.Importing += new EventHandler(DataImporter_Importing);
        }

        public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
        {
            DataImporter.Importing -= new EventHandler(DataImporter_Importing);
        }

        void DataImporter_Importing(object sender, EventArgs e)
        {
            DynamicDataTransferHandler ddsHandler = 
                (sender as DataImporter).TransferHandlers.Where(
                th => th.GetType() == typeof(DynamicDataTransferHandler)).Single() as DynamicDataTransferHandler;

            ddsHandler.DeserializedObject += delegate(object source, SerializationEventArgs args)
            {
                MyType instance = args.ObjectInstance as MyType;
                if (instance != null)
                {
                    //Check if referenced page guid have got new value, if so update reference
                    Guid mappedGuid;
                    if (ddsHandler.TransferContext.LinkGuidMap.TryGetValue(instance.PageGuid, out mappedGuid))
                    {
                        instance.PageGuid = mappedGuid;
                    }
                }
            };
        }

        public void Preload(string[] parameters){}
    }
Feb 25, 2011

Comments

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024