Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Johan Björnfot
Feb 25, 2011
  4971
(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
Optimizely CMS easy RSS feed integration library

As I've mentioned in my  previous blog post , while I was developing the Optimizely version of my blog, I tried to look for a library that could...

David Drouin-Prince | Jan 25, 2025 | Syndicated blog

Decimal numbers in Optimizely Graph

Storing prices as decimal numbers on a commerce website and planning to expose them through Optimizely Graph? It might not be as straightforward as...

Damian Smutek | Jan 23, 2025 | Syndicated blog

Find and delete non used media and blocks

On my new quest to play around with Blazor and MudBlazor I'm going back memory lane and porting some previously plugins. So this time up is my plug...

Per Nergård (MVP) | Jan 21, 2025

Optimizely Content Graph on mobile application

CG everywhere! I pull schema from our default index https://cg.optimizely.com/app/graphiql?auth=eBrGunULiC5TziTCtiOLEmov2LijBf30obh0KmhcBlyTktGZ in...

Cuong Nguyen Dinh | Jan 20, 2025

Image Analyzer with AI Assistant for Optimizely

The Smart Image Analyzer is a new feature in the Epicweb AI Assistant for Optimizely CMS that automates the management of image metadata, such as...

Luc Gosso (MVP) | Jan 16, 2025 | Syndicated blog

How to: create Decimal metafield with custom precision

If you are using catalog system, the way of creating metafields are easy – in fact, you can forget about “metafields”, all you should be using is t...

Quan Mai | Jan 16, 2025 | Syndicated blog