Monday, July 23, 2012

Extracting data from the Sitecore Web Forms for Marketers Module

The Web Forms for Marketers module for Sitecore comes with an extensive reporting tool inside the Sitecore Desktop. Information about submissions and activity on forms can be viewed in the reports already provided by the module. Recently, I had to create a custom report on WFFM forms that would live outside of the Desktop, so I decided to put together the basics on accessing form items and entries.
You would need a reference to the Sitecore.Forms.Core.dll

private void Example()
{
     string formID = "{FCD67950-6473-4962-B090-B4821BDB2C80}";
     ItemUri uri = new ItemUri(Sitecore.Data.ID.Parse(formID), Sitecore.Context.Database);

     //1. Get Form
     FormItem form = new FormItem(Database.GetItem(uri));
     string name = form.FormName;

     //2. Data Filters
     List<GridFilter> filters = new List<GridFilter>();
     // 2.a Form filter
     filters.Add(new GridFilter(Sitecore.Form.Core.Configuration.Constants.DataKey, formID, GridFilter.FilterOperator.Contains));
     // 2.b Get archived items
     filters.Add(new GridFilter(Sitecore.Form.Core.Configuration.Constants.StorageName, Sitecore.Form.Core.Configuration.Constants.Archive, GridFilter.FilterOperator.Contains));
            
     //3. Get all entries
     IEnumerable<IForm> entries = Sitecore.Forms.Data.DataManager.GetForms().GetPage(new PageCriteria(0, 0x7ffffffe), null, filters);

     // 3.a Apply custom filtering on the entries
     entries = entries.Where(a => a.Timestamp.Date.CompareTo(startDate) >= 0 && a.Timestamp.Date.CompareTo(endDate) <= 0);
            
     //4. Create a form packet
     FormPacket packet = new FormPacket(entries);

     CustomProcessor export = new CustomProcessor();
     string result = export.Process(form, packet);
}

1. Get the form
Use the FormItem constructor, passing in the inner data item, which, like any other item, can be grabbed based on its ID from the Context.Database. The FormItem class will give you access to various form properties such as the form.Introduction, form.Footer, and form.FormName as well as all the form fields and save actions.


2. Data filters
There are two filters that are obligatory in order to use the Sitecore.Forms.Data.DataProvider. To grab entries for a specific form, you will need to apply a GridFilter on the DataKey ("dataKey") field where the criteria would be the ID of the form. And the second filter specifies whether you are getting entries out of the archive or not. 

3. Get the entries
Having defined the grid filters, you can now grab all entries using the DataManager.

4. Create a form packet
The FormPacket makes it easy to ship a packet of entries that you've already filtered out off to a processor. For example, this could be a processor for a specific file type.

No comments:

Post a Comment