Skip to content

Instantly share code, notes, and snippets.

@Dheer-Rajpoot
Last active September 17, 2017 06:25
Show Gist options
  • Save Dheer-Rajpoot/465f989c08ab1f3179bb18aa03be8455 to your computer and use it in GitHub Desktop.
Save Dheer-Rajpoot/465f989c08ab1f3179bb18aa03be8455 to your computer and use it in GitHub Desktop.
WFFM | Save to Database Action
using Sitecore.Data;
using Sitecore.Diagnostics;
using Sitecore.WFFM.Abstractions.Actions;
using Sitecore.WFFM.Abstractions.Analytics;
using Sitecore.WFFM.Abstractions.Dependencies;
using Sitecore.WFFM.Actions.Base;
using Sitecore.WFFM.Analytics.Providers;
using System;
using System.Collections.Generic;
using System.Web;
namespace Website.WFFM.Actions
{
public class SaveToDatabaseAction : WffmSaveAction
{
private string connnectionString = Sitecore.Configuration.Settings.GetSetting("WFM.ConnectionString");
public override void Execute(ID formId, AdaptedResultList adaptedFields, ActionCallContext actionCallContext = null, params object[] data)
{
try
{
Assert.ArgumentNotNull(adaptedFields, "adaptedFields");
SqlFormsDataProvider provider = new SqlFormsDataProvider(!string.IsNullOrEmpty(connnectionString) ? connnectionString : "wffm", DependenciesManager.Settings, new SqlConnectionProvider());
Assert.ArgumentNotNull(provider, "provider");
FormData formData = new FormData();
formData.Timestamp = DateTime.Now;
formData.FormID = formId.Guid;
CreateFormFields(ref formData, adaptedFields);
provider.InsertFormData(formData);
Log.Audit("SaveToDatabaseAction : Form data is successfully saved to db ", this);
}
catch (Exception e)
{
Log.Error("SaveToDatabaseAction : Exception occured while saving Form data " + e.Message, this);
}
}
public void CreateFormFields(ref FormData form, AdaptedResultList adaptedFields)
{
List<FieldData> fieldDataList = new List<FieldData>();
var request = HttpContext.Current.Request;
foreach (AdaptedControlResult field in adaptedFields)
{
FieldData fieldData = new FieldData()
{
Id = Guid.NewGuid(),
FieldId = new Guid(field.FieldID),
FieldName = field.FieldName,
Value = field.Value,
Data = null
};
fieldDataList.Add(fieldData);
}
FieldData browserFieldData = new FieldData()
{
Id = Guid.NewGuid(),
FieldId = new Guid("{2908AEEB-8663-4127-9BFD-FD7E6FCE2602}"),
FieldName = "BROWSER DETAILS",
Value = "Browser Name : " + request.Browser.Browser + " Browser Version : " + request.Browser.Version,
Data = null
};
fieldDataList.Add(browserFieldData);
form.Fields = fieldDataList;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment