Tagged: FormSubmitContext

Generic method to read values from any type of Form fields

Are you looking for a generic method to read the values from any type of Sitecore Form fields? Here is the solution.

         private static Dictionary<Guid, string> FormFieldsToDictionary(IList<IViewModel> fields)
		{
			Dictionary<Guid, string> fielDictionary = new Dictionary<Guid, string>();
			foreach (var field in fields)
			{
				fielDictionary.Add(Guid.Parse(field.ItemId), field.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty);
			}
			return fielDictionary;
		}
	private static string GetValue(IViewModel field)
		{
			if (field == null)
			{ return default(string); }


			if ((field as StringInputViewModel) != null)
			{
				return (string)(object)(field as StringInputViewModel).Value;
			}

			if (field is ListViewModel)
			{
				var listField = (ListViewModel)field;
				var array = listField?.Value?.ToArray();
				if (array == null)
				{
					return string.Empty;
				}
				return String.Join(",", array);
			}

			if (field is DateViewModel)
			{
				var dateField = (DateViewModel)field;
				return dateField.Value.HasValue ? dateField.Value.Value.ToShortDateString() : string.Empty;
			}

			if (field is NumberViewModel)
			{
				var numberField = (NumberViewModel)field;
				return numberField.Value.HasValue ? numberField.Value.ToString() : string.Empty;
			}

			if (field is TextViewModel)
			{
				var textField = (TextViewModel)field;
				return (string)(object)textField.Text;
			}

			if (field is CheckBoxViewModel)
			{
				var checkbox = (CheckBoxListViewModel)field;
				return (string)(object)checkbox.Value;
			}


			return default(string);
		}

Utilization:

protected override bool Execute(UpdateContactData data, FormSubmitContext formSubmitContext)
		{
			var fieldsDictionary = FormFieldsToDictionary(formSubmitContext.Fields);

           // From here you can create a model from fieldsDictionary, call any API and return call status

       }

Hope this helps someone. Any questions, leave a comment.

Happy Sitecoring!

0

Sitecore 9 Forms: Access landing page fields in FormSubmitContext?

I came across a scenario where I needed to access landing page fields(the page where we add Forms) in Custom Submit Action’s FormSubmitContext. 

By default, the page item isn’t known at the Submit Action. So started thinking should i create a custom hidden field or use the Forms Extensions module? At the end of the day, i didn’t need either. 

Here is the quick way to access the current page using HttpContext’s AbsolutePath.

protected override bool Execute(string data, FormSubmitContext formSubmitContext)
{
 
var contextItem = GetContextItem.GetItem(HttpContext.Current.Request.UrlReferrer?.AbsolutePath) as ILandingPageContentItem;

// Access fields from ContextItem
}

public class GetContextItem
{
	public static IStandardTemplateItem GetItem(string path)
	{
		Item item = Sitecore.Context.Database.GetItem($"/sitecore/content/<tenant>/Home{path}");
		
		return item?.AsStronglyTyped();
	}
}

Hope this helps someone. Any questions, please leave a comment.

Happy Sitecoring!

0