Storing data using EPiServer 7.5 Dynamic Data Store

image_212Sometimes you want to able to save data that is not a page or a content block. Examples could be data like comments created by visitors or orders in your webshop. An easy way to save such data is using EPiServers build-in Dynamic Data Store.

There are a lot of blogs online about this subject. Unfortunality most of them only work in EPiServer 6. In this blog I took a look at Ted Nyberg’s blog Introduction to EPiServer Dynamic Data Store (DDS) but rewrote it so that it now works for EPiServer 7.5.

I am going to store comments created by my visitors using a Dynamic Data Store. First we need a model to hold the data. Create your model in the ~/Models/DynamicDatadirectory or any directory you prefer.

This model is not complete yet! But it summarizes the data I like to store. What is important here is that your model inherits from IDynamicData and that you have put some references in your code to EPiServer.Data and EPiServer.Data.Dynamic.

You can put whatever property you want in your model. But the Identity Id property is required to identify this comment in the store.

Now lets create a contructor and some initialisation methods:

ote that a public, parameter-less constructor is required to support serialization to the Dynamic Data Store (Ted Nyberg)

Next lets make some methods that savesdeletes and retrieves comments.

Notice that I use DynamicDataStoreFactory to create a store. Also retrieving comments is done differently.

Ok thats it. Now we can save comments with this code:

And retrieving comments is as easy as saving them:

Changing or updating your Dynamic Data Store model

So what happens when you update your comment model. For example you add an extra property email address that stores the email address of the visitor who added the comment. If you do that and rebuild the project and try to retrieve the comments you will get an error:

StoreInconsistencyException in EPiServer

StoreInconsistencyException in EPiServer

To solve this you need to remap your Dynamic Data Store. This can be done automatically everytime you change your model using the attribute:

[EPiServerDataStore(AutomaticallyRemapStore = true)]

Be carefull! Remapping can lead to data loss.

Other attributes that could be handy are:

[EPiServerDataStore(AutomaticallyCreateStore = true)]

Setting this boolean property to true means that you no longer need to worry if your store exists or not, it will be created on demand when GetStore is called!

Leave a Comment.