Skip to main content

Back-End Development

Sitecore 9 Forms – Creating and Adding Custom Actions

Website Construction Line Style Illustration

Welcome to the second post in my series on Sitecore Forms!  In this article, we’ll dig into creating and adding custom actions.

As a first step, you can use the submit button for navigation, to submit the form, and/or to trigger submit actions. The next step is, of course, customization – you can create your own custom submit actions. For example, the submit action Save data added to the next button ensures that the data is saved to your database when a contact clicks Next to move on to the next page of the form.

                           
    

So how does it work and what does it mean for you? You can create a submit button that can perform multiple actions. For example, if you have a multipage form, you can add an action to each of the submit buttons to notify you via email when each page is submitted.

Let’s start by creating a custom class, and name it EmailMe. This newly created class has to inherit from SubmitActionBase<Parameters>.

Below, you can see an example of a code that was used for this task. It’s important to note that if you are using a string type parameter in the SubmitActionBase,  you’ll need to use the protected override bool TryParse method. Whenever you click the submit button, you’ll get the error message that was set in the Error Message field of the custom submit action.

//Add Libraries necessary for the project
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Processing;
using Sitecore.ExperienceForms.Processing.Actions;
using System.Linq;

namespace &amp;lt;ProjectName&amp;gt;.Models
{

//Executes a submit action for logging the form submit status.
   public class EmailMe : SubmitActionBase&amp;lt;string&amp;gt;
{


    //Initializes a new instance of the EmailMe class.
    //submitActionData =&amp;gt; The submit action data
    public EmailMe(ISubmitActionData submitActionData) : base(submitActionData)
    {
    }


    //Executes the action with the specified &amp;lt;paramref name="data" /&amp;gt;.
    //&amp;lt;param name="data"&amp;gt;The data.&amp;lt;/param&amp;gt;
    //&amp;lt;param name="formSubmitContext"&amp;gt;The form submit context.&amp;lt;/param&amp;gt;
    //returns &amp;lt;c&amp;gt;true&amp;lt;/c&amp;gt; if the action is executed correctly; otherwise &amp;lt;c&amp;gt;false&amp;lt;/c&amp;gt;
    protected override bool Execute(string data, FormSubmitContext formSubmitContext)
    {
        //The code that will be executed will go here 
        string Email = "Email";
        Email = GetValue(formSubmitContext.Fields.FirstOrDefault(f =&amp;gt; f.Name == "EmailMe"));
        return true;
    }
    //Tries to convert the specified &amp;lt;paramref name="value" /&amp;gt; to an instance of the  specified target type.
    //&amp;lt;param name="value"&amp;gt;The value.&amp;lt;/param&amp;gt;
    //&amp;lt;param name="target"&amp;gt;The target object.&amp;lt;/param&amp;gt;
    //returns true if &amp;lt;paramref name="value" /&amp;gt; was converted successfully; otherwise, false.    
    protected override bool TryParse(string value, out string target)
    {
        target = string.Empty;
        return true;
    }
  }
}

The class you create can be placed wherever you want in your project. If you are using SCORE, I suggest you place it with other Models.

            

After adding the class build to your project –  and assuming you’ve done everything correctly – once you go back to creating forms you will notice a new action under your submit actions.

You can also add a custom icon or custom validation (we’ll talk about that in another post!), but for now you can add a custom error message as a way to check to see if your form is working.

Hint: Once you add the form to your page and click the submit button, you will see the error message so you know you did something wrong.

After this step, go to your Sitecore instance and navigate to System-> Settings-> Forms-> Submit Actions and add an action form submit action template.

Insert a model into the model,  for example:<ProjectName>.Models.EmailMe.

Now go back to forms and try to create a new one. You’ll notice that your custom Submit Action has been made.

HINT: After adding your new class, build the project, add your submit action to your instance and publish it!

Sitecore 9 Forms offer a lot of opportunities for customization and I hope you’ve learned a few basic guidelines on how to use them.

Happy coding!

Dragana

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Dragana Simonovic

Dragana is a newly graduated engineer of Mechatronics and a Sitecore enthusiast and certified SCORE developer. She hopes her posts will help people who are just entering the world of SCORE and Sitecore. The programming technologies Dragana has experience with include MVC.NET, JavaScript, jQuery, and C#. In her free time, she enjoys traveling and reading.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram