EPiServer : Dynamic Data Store

If you are reading this post, then I can assume you are curios about EPiServer dynamic data store and want to know how to use it. In this post, I am going to do exactly the same. I will create, update and delete from DDS using EPiServer API.

1- Create a new dynamic data store

Imagine you have to dynamically store landing page ids for certain products or sections and then read them back from dynamic data store

private void CreateLandingPageDDS(PageData myPage)
{
//create a propertybag
PropertyBag landingProperties = new PropertyBag();

//add your custom properties
landingProperties.Add("LandingPageKey", myPage.PageLink.ID.ToString());
landingProperties.Add("LandingPageProductName", myPage.PageName);
//add more properties if you like, but there is a limit that you can add upto, or
//create a custom object and add that object

//create a new store for DDS
DynamicDataStore store = DynamicDataStoreFactory.Instance.CreateStore("DDS",   landingProperties.GenerateTypeBag());

//store DDS in database
Identity id = store.Save(landingProperties);

}

pretty simple 🙂

2- Get dynamic data store

Now let say you want to query the same DDS and would like to get pageName if you pass in the pageID

public static string GetLandingPageName(string currentPageID)
{
//get the store from database
DynamicDataStore store = (DynamicDataStore)DynamicDataStoreFactory.Instance.GetStore("DDS");

if (store != null)
{
//find all the properties for that key/value pair
IEnumerable<PropertyBag> landingProperties = store.FindAsPropertyBag("LandingPageKey", currentPageID);

//iterate through each property and return only where key matches
if (landingProperties != null)
{
foreach (PropertyBag item in landingProperties)
{
string pageName = item["LandingPageProductName"] as string ?? string.Empty;
return pageName;
}

}

}
return string.Empty;
}

Although pageName is a trivial property, this is just an example how to go about using DDS.

3- Update dynamic data store

Now let say you want to update pageName using pageID

private void UpdateLandingPage(string currentPageID)
{
DynamicDataStore store = (DynamicDataStore)DynamicDataStoreFactory.Instance.GetStore("DDS");

if (store != null)
{
IEnumerable<PropertyBag> landingProperties = store.FindAsPropertyBag("LandingPageKey", currentPageID);

if (landingProperties != null)
{
foreach (PropertyBag item in landingProperties)
{
item["LandingPageProductName"] = "New name";
store.Save(item);
}


}

}
}

4- Delete dynamic data store

That is very strait forward, just use

DynamicDataStoreFactory.Instance.DeleteStore("DDS",true);

5-Important note

If during development, you have to change your dynamic data store by adding more properties to property bag object, it will result in exception.

There are two ways to get round this

1.If you are in development, delete your previous DDS entries and start fresh
2.If you are in production, then you have to re-map/re-link the property bag.

Have fun coding DDS

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s