Using the dynamic data store with XForms
In EPiServer CMS 6 the data storage of XForms and XForm postings have been moved from “object store” to the new dynamic data store (DDS). This has a lot of advantages. Object store stored it’s objects in serialized form which did not allow fast lookups and searches for objects in the database. For instance, when calculating form statistics in EPiServer CMS 5, we had to load all form postings from the database, deserialize them and then calculate the statistics. Since object store did not a built in cache this was a pretty heavy operation for forms with a lot of posts.
The form statistics has also been rewritten so that the calculation is done in the database which should remove the need for developers on sites with forms with a lot of postings to add a cache layer for statistics. The form data tab in edit mode has also been rewritten to utilize database paging when browsing through form postings.
Searching form postings
I was a bit curious on what new capabilities that this change gave for the developer so I sat down yesterday for a few hours to test if I could do LINQ to SQL queries against the form postings as a site developer. I started writing a method that would get the count of postings that had the “email” field ending with a specified value as I would have done in EPiServer CMS 5.
1: public static int GetNumberOfPostsWithGivenEmailEndingV1(XForm form, string stringToMatch)
2: {
3: IList<XFormData> formData = form.GetPostedData();
4:
5: var a = from item in formData
6: where item.GetValue("Email").EndsWith(stringToMatch)
7: select item;
8:
9: return a.Count<XFormData>();
10: }
Now I tried to do the same using LINQ to SQL:
1: public static int GetNumberOfPostsWithGivenEmailEndingV2(XForm form, string stringToMatch)
2: {
3: DynamicDataStore store = XFormData.GetStore((Guid)form.Id);
4:
5: return (from post in store.ItemsAsPropertyBag()
6: where ((string)post["Email"]).EndsWith(stringToMatch)
7: select post).Count();
8: }
Note that I’m using the ItemAsPropertyBag method to be able to seach the form fields. I’m also using the form id as an identifier to get the store.
Adding custom data
Since XForm postings saved it’s data mostly as xml it was possible to add data that did not match a form field in EPiServer CMS 4/5. In the DDS, each form field is mapped against a table column and a view is set up for each form to be able to quickly search and fetch data for a form. To still allow custom data to be stored to a form posting, a Dictionary<string, object> was added to the XFormData class. This makes it possible to store custom data as typed objects instead of strings. Here is an example how to add custom data by attaching an event handler to the BeforeSubmitPostedData event on an XFormControl instance.
1: protected void FormControl_BeforeSubmitPostedData(object sender, SaveFormDataEventArgs e)
2: {
3: int randomValue = new Random().Next(100);
4: //Using string value
5: e.FormData.SetValue("RandomValue", randomValue.ToString());
6: //Using typed value
7: e.FormData.CustomData["RandomValue"] = randomValue;
8: }
I tried doing a search against the custom data by using LINQ to Objects:
1: public static int GetNumberOfPostsWithRandomValueOverThreshold(XForm form, int threshold)
2: {
3: IList<XFormData> formData = form.GetPostedData();
4:
5: var a = from item in formData
6: where ((int)item.CustomData["RandomValue"]) > threshold
7: select item;
8:
9: return a.Count<XFormData>();
10: }
Last of all I wanted to execute the same search by using LINQ to SQL. Here my success came to an halt though. Since the custom data is stored in a field of type Dictionary<string, object> this means that I have to do a search using nested property bags. This is a feature currently not supported by the DDS.
Conclusions: It’s possible to use LINQ to SQL to seach for form postings that match a given criteria in EPiServer CMS 6, although it’s currently not possible to search on custom data.
Comments