November Happy Hour will be moved to Thursday December 5th.

Johan Björnfot
Feb 25, 2011
  4946
(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 SaaS CMS + Coveo Search Page

Short on time but need a listing feature with filters, pagination, and sorting? Create a fully functional Coveo-powered search page driven by data...

Damian Smutek | Nov 21, 2024 | Syndicated blog

Optimizely SaaS CMS DAM Picker (Interim)

Simplify your Optimizely SaaS CMS workflow with the Interim DAM Picker Chrome extension. Seamlessly integrate your DAM system, streamlining asset...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Optimizely CMS Roadmap

Explore Optimizely CMS's latest roadmap, packed with developer-focused updates. From SaaS speed to Visual Builder enhancements, developer tooling...

Andy Blyth | Nov 21, 2024 | Syndicated blog

Set Default Culture in Optimizely CMS 12

Take control over culture-specific operations like date and time formatting.

Tomas Hensrud Gulla | Nov 15, 2024 | Syndicated blog