Sitecore Web Forms For Marketers (WFFM) retrieve data using code

Jignesh Patel.NET, Sitecore, Sitecore MVC0 Comments

Retrieve Web Forms For Marketers (WFFM) data using C#

Web Forms For Marketers (WFFM) data will be extracted using following code for Sitecore Ver. 8.2

IWffmDataProvider provider = DependenciesManager.DataProvider;
IEnumerable data = provider.GetFormData(<form/item id>);

Following code will help for testing functionality

Code file: IWffmDataRepository.cs

using System.Collections.Generic;
using Sitecore.Data.Items;
using Sitecore.WFFM.Abstractions.Analytics;

namespace Sitecore.Feature.CustomForms.Repositories
{
    public interface IWffmDataRepository
    {
        IEnumerable GetFormData(Item formItem);
    }
}

Code file: WffmDataRepository.cs

using System.Collections.Generic;
using Sitecore.Data.Items;
using Sitecore.WFFM.Abstractions.Analytics;
using Sitecore.WFFM.Abstractions.Dependencies;

namespace Sitecore.Feature.CustomForms.Repositories
{
    public class WffmDataRepository : IWffmDataRepository
    {
        private readonly IWffmDataProvider _dataProvider;

        public WffmDataRepository(): this(DependenciesManager.DataProvider)
        {
        }

        public WffmDataRepository(IWffmDataProvider dataProvider)
        {
            _dataProvider = dataProvider;
        }

        public IEnumerable GetFormData(Item formItem)
            => _dataProvider.GetFormData(formItem.ID.ToGuid());
    }
}

How to get all WFFM items for current site

var site = SiteContext.Current;
Assert.IsNotNull(site, "SitecoreContext.Current is null");

var formsRoot = SiteUtils.GetFormsRootForSite(site);
Assert.IsNotNull(site, "Forms Root for Site is null");

var repo = new WffmDataRepository();

ChildList formItems = MasterDatabase.GetItem(formsRoot).GetChildren();
foreach (Item item in formItems)
{
	var formData = repo.GetFormData(item);
	//
	//
}

Hope that helps but feel free let me know if you have any question.

Leave a Reply

Your email address will not be published. Required fields are marked *