How to get Form Field Value Sitecore 9.0.1

 

I am using Sitecore 9 forms and i have created a a custom save action and my requirement is i need to get Form feed value in my CustomSaveAction class which inherits SubmitActionBase class.

I am able to see the data in formSubmitContext object but not able to retrieve the data.

Thanks

  • You can take a look at this Save action:

    github.com/.../AzureQueueSubmit.cs

    You need somethings like this:

     

    public class YourSaveActionSubmit : SubmitActionBase<string>
    {
    
     ....
     ....
     
     
            /// <summary>
            /// Executes the action with the specified <paramref name="data" />.
            /// </summary>
            /// <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>
            /// </returns>
            protected override bool Execute(string data, FormSubmitContext formSubmitContext)
            {
                
                foreach (var viewModel in formSubmitContext.Fields)
                {
                    var postedField = (IValueField) viewModel;
                    IValueField valueField = postedField as IValueField;
                    PropertyInfo property = postedField.GetType().GetProperty("Value");
                    object postedValue =
                        (object) property != null ? property.GetValue((object) postedField) : (object) null;
                    property = postedField.GetType().GetProperty("Title");
                    object postedTitle =
                        (object) property != null ? property.GetValue((object) postedField) : (object) null;
                    if (postedValue != null && postedTitle != null)
                    {
                        //And Here is the retrieved form data
                        var allowedtosave = valueField.AllowSave;
                        var Name = viewModel.Name;
                        var Title = postedTitle.ToString();
                        var ItemId = viewModel.ItemId;
                        var Value = postedValue.ToString();
                    }
                }
                return true;
            }
            
            
    }

     

    Also take a look at the Sitecore documentation about the custom submit actions:

    https://doc.sitecore.com/developers/90/sitecore-experience-management/en/walkthrough--creating-a-custom-submit-action.html

    https://doc.sitecore.com/developers/90/sitecore-experience-management/en/walkthrough--creating-a-custom-submit-action-that-updates-contact-details.html