How to Create Sitecore Web Form
GOAL: To Create simple sitecore WebForms and store values in custom DB
Design:
- Design the webform based on requirement and suitable field drag and drop.
- Create Submit Button Action method and provide Model Type Field values.
- Tag newly created action method to webform submit button.
- Install below mentioned sitecore Nuget package related to webform.
Sitecore.ExperienceFormsSitecore.ExperienceForms.AnalyticsSitecore.ExperienceForms.ClientSitecore.ExperienceForms.Data.SqlServerSitecore.ExperienceForms.MvcSitecore.ExperienceForms.SubmitActions
Code Logic:
public class ProfileForm : SubmitActionBase<string>
{ string Name = string.Empty;
string Email = string.Empty;
string Address = string.Empty;
//submitActionData => The submit action data
public ProfileForm(ISubmitActionData submitActionData) : base(submitActionData)
{
}
//Executes the action with the specified <paramref name="data" />.
//<param name="data">The data.</param>
//<param name="formSubmitContext">The form submit context.</param>
//returns <c>true</c> if the action is executed correctly; otherwise <c>false</c>
protected override bool Execute(string data, FormSubmitContext formSubmitContext)
{ bool result = false;
try
{
var formFields = formSubmitContext.Fields;
Name = GetValue(GetFieldByName("Name", formFields));
Email = GetValue(GetFieldByName("Email", formFields));
Address = GetValue(GetFieldByName("Address", formFields));
result = SaveData(Name, Email, Address);
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error Occured in Execute method of Profile", ex.Message);
}
return result;
}
//Tries to convert the specified <paramref name="value" /> to an instance of the specified target type.
//<param name="value">The value.</param>
//<param name="target">The target object.</param>
//returns true if <paramref name="value" /> was converted successfully; otherwise, false.
protected override bool TryParse(string value, out string target)
{
target = string.Empty;
return true;
}
public static IViewModel GetFieldByName(string name, IList<IViewModel> fields)
{
return fields.FirstOrDefault(f => ((Sitecore.ExperienceForms.Mvc.Models.Fields.TitleFieldViewModel)f).Title == name);
}
public static string GetValue(IViewModel postedField)
{
Assert.ArgumentNotNull((object)postedField, "postedField");
var property = postedField.GetType().GetProperty("Value");
object obj;
if (property == null)
{
obj = (object)null;
}
else
{
IViewModel viewModel = postedField;
obj = property.GetValue((object)viewModel);
}
var postedValue = obj;
if (postedValue == null)
{ return string.Empty;
}
var parsedValue = ParseFieldValue(postedValue);
return parsedValue;
}
public static string ParseFieldValue(object postedValue)
{
Assert.ArgumentNotNull(postedValue, "postedValue");
var list = new List<string>();
if (postedValue is IEnumerable<object> secondList)
{
foreach (object obj in secondList)
{
list.Add(obj.ToString());
}
}
else
{ list.Add(postedValue.ToString());
}
return string.Join(",", list);
}
private bool SaveData(string Name, string Email, string Address)
{ bool result = false;
try
{ // Accessing Sql Custom DB to Store form values
using (var profile = new Sitecore_CustomEntities())
{
Profile pRecord = new Profile();
pRecord.Profile_Id = Guid.NewGuid();
pRecord.Name = Name;
pRecord.Email = Email;
pRecord.Address = Address;
pRecord.Modified_Date = DateTime.UtcNow;
pRecord.Created_Date = DateTime.UtcNow;
profile.profiles.Add(pRecord);
result = profile.SaveChanges();
}
}
catch (Exception ex)
{
Sitecore.Diagnostics.Log.Error("Error Occured in SaveData method of Profile Functionality ...", ex.Message);
}
return result;
}
}
Happy Coding!!!☺
Thanks for reading…..!!!Stay tuned for more….!!!
Comments
Post a Comment