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

     

    Note: this is something dated, now there are also lists, and even files possible in Sitecore 9.3, below is a new example

  • In reply to Jan Bluemink:

    We are using Sitecore 9.1.1 and I implemented this code which works fine on most single line text fields however when attempting to get the value from a field selected that is a DropDownList and having issues.

    Sample Code:

    .

    .

    var postedField = (IValueField)viewModel;
    PropertyInfo property = postedField.GetType().GetProperty("Value");
    object postedValue = (object)property != null ? property.GetValue((object)postedField) : (object)null;

    if (postedValue != null)
    {
    var SelectedValue = postedValue.ToString();

     

    Attaching screen shot of the Watch window:

     

    Thoughts?

     

  • In reply to Daniel Kyte:

     I currently use this code that check on list fields, I have not tested it on Sitecore 9.1. only on a higher version but I assume this does the trick also on your version. So first call the ParseFieldValue to check for lists, and if a list convert to a string value.

     

            private static void AddFieldData(IViewModel viewModel, FormFields message)
            {
                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 (valueField.AllowSave && postedValue != null && postedTitle != null)
                {
                    message.Fields.Add(new FormFieldSmall()
                    {
                        Name = viewModel.Name,
                        Title = postedTitle.ToString(),
                        ItemId = viewModel.ItemId,
                        Value = ParseFieldValue(postedValue)
                    });
                }
            }
    
            protected static string ParseFieldValue(object postedValue)
            {
                Assert.ArgumentNotNull(postedValue, nameof(postedValue));
                List<string> stringList = new List<string>();
                IList list = postedValue as IList;
                if (list != null)
                {
                    foreach (object obj in (IEnumerable)list)
                        stringList.Add(obj.ToString());
                }
                else
                    stringList.Add(postedValue.ToString());
                return string.Join(",", (IEnumerable<string>)stringList);
            }

  • In reply to Jan Bluemink:

    Thank you Jan for sending this information, we ended up going with this solution:

    DropDownListViewModel droplist = formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals(viewModel.Name, StringComparison.InvariantCultureIgnoreCase)) as DropDownListViewModel;

    var SelectedValue = (droplist.Items.FirstOrDefault(i => i.Selected)).Value;