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.


Implementation:
  • Install below mentioned sitecore Nuget package related to webform.
Sitecore.ExperienceForms
Sitecore.ExperienceForms.Analytics
Sitecore.ExperienceForms.Client
Sitecore.ExperienceForms.Data.SqlServer
Sitecore.ExperienceForms.Mvc
Sitecore.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

            Popular posts from this blog

            Hide Sitecore Rendering Based On Querystring