Add Custom Save Button in Sitecore 9.3 Forms



In Sitecore Forms we can easily add custom save button to manipulate forms data,
Well i had a requirement to capture some data from forms and insert it under custom database.  So its allow to override this one!

let's start to create logic for capture data!
Just create a MVC solution and add refrence Sitecore.Kernel, Sitecore.Mvc and Sitecore.ExperienceForms.dll

after that just create a class and inherit  by SubmitActionBase. you need to add some more references in your class.

using Sitecore.Diagnostics;
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Processing.Actions;
using Sitecore.ExperienceForms.Processing;




Below code you need to add under your class to execute your custom button.


 public forms(ISubmitActionData submitActionData) : base(submitActionData)
        {
        }


        public override void ExecuteAction(FormSubmitContext formSubmitContext, string parameters)
        {
            Assert.ArgumentNotNull((object)formSubmitContext, nameof(formSubmitContext));

            if (this.TryParse(parameters, out string target))
            {
                try
                {
                    if (this.Execute(target, formSubmitContext))
                        return;
                }
                catch (ArgumentNullException ex)
                {
                    throw ex;
                }
            }
            formSubmitContext.Errors.Add(new FormActionError()
            {
                ErrorMessage = this.SubmitActionData.ErrorMessage
            });
        }

        protected override bool Execute(string data, FormSubmitContext formSubmitContext)
        {
            Assert.ArgumentNotNull(data, nameof(data));
            Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));

           
            //Get the sitecore forms field value here
            string Email = GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("EmailAddress")));
           

            // Call any method to save data

            return true;
        }

        private static string GetValue(object field)
        {
            return field?.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty;
        }

        protected override bool TryParse(string value, out string target)
        {
            target = string.Empty;
            return true;
        }




So almost done coding part now moving into Sitecore to create custom button,



we need to Navigate below path to create custom button

/sitecore/system/Settings/Forms/Submit Actions

just right click on submit action folder and add submit button and add name as you want. once button created then you need to add your models here looks like





just ave it and publish this folder!  now navigate on forms where you want to add custom button, it will display under Submit Action list,





select this one and save the form, publish and attach the worker process to cross verify,
That's the simple method to capture Sitecore forms data.


Reference:
https://doc.sitecore.com/developers/93/sitecore-experience-manager/en/add-a-form-to-a-webpage.html
https://doc.sitecore.com/developers/90/sitecore-experience-manager/en/walkthrough--creating-a-custom-submit-action-that-updates-contact-details.html


Hope it will be helpfull😇

Comments

Popular posts from this blog

Difference between shared, Versioned and Unversioned fileds in sitecore

How to find broken links in Sitecore.

Setup First Sitecore Helix Example From Scratch (Blank solution)