Skip to main content

Create a new save action

Abstract

Create a custom save action using base interface and classes.

A save action is the second of three submit actions that are performed when a website visitor submits a web form. A save action is performed after all verification actions are successfully completed.

There are several default save actions that are part of the Web Forms for Marketers module. However, you can also create your own custom save actions by using the interface and base classes to create a new class.

To create a custom save action, you must create a class that inherits the Sitecore.WFFM.Abstractions.Actions.ISaveAction interface or the Sitecore.WFFM.Actions.Base.WffmSaveAction abstract class. The interface and abstract class contain the Execute method that must be implemented in your new class because it is called by all the save actions that are assigned to a web form.

To create a new save action:

  1. Create a new project in Visual Studio, for example, Sitecore.Forms.Sample.

  2. Add a new reference to the Sitecore.WFFM.Abstractions assembly. If the save action is derived from the WffmSaveAction class, you must add a new reference to the Sitecore.WFFM.Actions assembly.

  3. Create a new class that inherits the Sitecore.WFFM.Abstractions.Actions.ISaveAction interface, for example:

    using Sitecore.Data;
    using Sitecore.Diagnostics;
    using Sitecore.Security.Authentication;
    using Sitecore.WFFM.Abstractions.Actions;
    namespace Sitecore.Forms.Sample
    {
      /// <summary>
      /// Login action 
      /// </summary>
      public class LoginAction : ISaveAction
      {
        /// <summary>
        /// Initializes a new instance of the <see cref="LoginAction"/> class.
        /// </summary>
        public LoginAction()
        {
          this.DefaultDomain = "sitecore";
        }
        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="formId">
        /// The form id.
        /// </param>
        /// <param name="adaptedFields">
        /// The adapted fields.
        /// </param>
        /// <param name="actionCallContext">
        /// The action call context.
        /// </param>
        /// <param name="data">
        /// The data.
        /// </param>
        public void Execute(ID formId, AdaptedResultList adaptedFields, ActionCallContext actionCallContext = null, params object[] data)
        {
          AdaptedControlResult login = adaptedFields.GetEntry(this.Login, "Login");
          AdaptedControlResult password = adaptedFields.GetEntry(this.Password, "Password");
          Assert.ArgumentNotNull(login, "You should fill in the 'Login' field.");
          Assert.ArgumentNotNull(password, "You should fill in the 'Password' field.");
          string userName = login.Value;
          Assert.ArgumentNotNullOrEmpty(userName, "The 'Login' field can't be empty");
          if (!userName.Contains("\\"))
          {
            userName = string.Join("\\", new[] { this.DefaultDomain, userName });
          }
          AuthenticationManager.Login(userName, password.Value, true);
        }
        /// <summary>
        /// Gets or sets the default domain.
        /// </summary>
        /// <value>The default domain.</value>
        public string DefaultDomain { get; set; }
        /// <summary>
        /// Gets or sets the login field.
        /// </summary>
        public string Login { get; set; }
        /// <summary>
        /// Gets or sets the password field.
        /// </summary>
        public string Password { get; set; }
        public ID ActionID { get; set; }
        public string UniqueKey { get; set; }
        public ActionType ActionType { get; private set; }
        public ActionState QueryState(ActionQueryContext queryContext)
        {
          return ActionState.Enabled;
        }
      }
    }
    
  4. Compile the class to an assembly and place it in your website's bin folder.

  5. In the content tree, navigate to sitecore/System/Modules/Web Forms for Marketers/Settings/Actions/Save Actions and in the right pane, create a new item by clicking the Save Action button.

  6. In the Message dialog, enter a name for the item, and then click OK.

  7. In the new save action item, fill in the Assembly and Class fields by entering the name of the assembly and class that you have created.

    You can further customize the save action by modifying the Parameters and Localized Parameters field.

  8. Click Save.