Menu Home

Adding custom information into submit action

What are submit actions? Sitecore introduced Sitecore Forms with Sitecore 9. They are designed to replace Sitecores Web Forms For Marketers (WFFM). These forms expose a concept of submit actions (if you are familar with WFFM, these are much like save actions). These are components that determine what happens when the submit button is selected. What is great about submit actions is that they are designed with seperation of concerns in mind. An action is built to handle a decrete task like:

  • Save information to a database
  • Redirect to a page

This provides a clear seperation of concerns and allows the developer to stack submit actions into different combinations to achieve a desired result. Information flows in one direction, from the first action to the last, and information is passed via the FormSubmitContext. Finally, you can create custom submit actions that account for tasks that are not available with the default submit actions like:

  • Identify a contact
  • Register a user

This brings us to our current situation. Recently, we were asked to create a form that registers an individual with a third party application and provide information about that registration within an email. Our obvious though is to create a custom submit action. This was easy, there is clear documention on creating a custom submit actions and we just hit a rest api provided by the third party application to register and obtain information about that registration but how do I get this information to the next submit action? There are a few ways to do this, but a quick an easy way is to add it the FormSubmitContext which will automatically carry the data though the process. This is done by adding information to the FormsSubmitContext.Fields collection. This collection accepts a ViewModel where the information you would like pass is added. Here is an example of how to do it:

//Adding data within a submit action
formSubmitContext.Fields.Add(new StringInputViewModel
{
	Name = "RegistrationId",
	Value = "4500a688-8fd7-4608-9755-a1edb6c21633"
});

//consuming data within a different submit action
var registerIdValue = GetFieldValueByName("RegistrationId", formSubmitContext.Fields);

//method to grab field
public static string GetFieldValueByName(string name, IList<IViewModel> fields)
{
     string Value = string.Empty;
     var field = fields.FirstOrDefault(f => f.Name.Trim().ToLower() == name.Trim().ToLower());
     if (field != null)
     {
	var fieldType = field?.GetType();
	Value =  fieldType.GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty;
     }
	return Value;
}

That is it! The information is now available within subsequent submit actions. I hope someone finds this tidbit useful.

Categories: C# Sitecore Forms

omcbride

15+ year programming professional with 10+ years devoted to Sitecore development. I've been using the platform since Sitecore 6.0 and currently serve as a Technical Architect at Verndale.

Leave a Reply

Your email address will not be published. Required fields are marked *