Passing subscription id to a custom subscribe action of WFFM

Posted 02/05/2014 by Piyada Cheilchanthong

In one project, I was assigned to work on the subscribe to newsletters using WFFM.  There are three different subscription pages that allowing users to subscribe to different subscription areas, each area is identified by subscription id.   

Since all three subscription pages look the same except that the user would be subscribed to different subscription areas so I was looking for a way to share the same WFFM and custom action and found a good source of information from Creating a custom action editor for WFFM blog posted by Matthew Schultz to accomplish as following.

  • Create a new save action called "Subscribe to NewsLetter" action under "/siteocre/system/Modules/Web Forms for Marketers/Settings/Actions/Save Actions", then creating a class that supports the subscribe action called "SubscribeNewsletterUser" that extends BaseSaveAction, and implement the Execute method.  Since I was interested in having the users define their own subscription id and pass it along with other WFM fields I create on the form, so I add subscriptionID property to my class.  Here is how my class looks like:

public class SubscribeNewsletterUser : BaseSaveAction
{
        public string SubscriptionID
        {
            get;
            set;
        }

        public override void Execute(ID formid, AdaptedResultList fields, params object[] data)
        {
            Subscribers subscribers = new Subscribers();

            subscribers.FirstName = fields.GetEntryByName("First Name").Value;

            subscribers.LastName = fields.GetEntryByName("Last Name").Value;

            subscribers.Email = fields.GetEntryByName("Email").Value;

            subscribers.Organization = fields.GetEntryByName("Organization").Value;

            AdaptedControlResult areaFld = fields.GetEntryByName("Areas Of Interest");

            if (!string.IsNullOrEmpty(SubscriptionID))
                subscribers.SubscriptionID= Convert.ToInt32(SubscriptionID);

            try
            {
                subscribers.CreateSubscriberNoList();

                try
                {
                    subscribers.SubscriberToList();
                }
                catch (Exception ex)
                {
                    Log.Error(string.Format( "An error occurred when creating the subscriber; {0}. Please contact the system administration for more details", ex.Message), this);

                    throw ex;
                }
            }
            catch (System.Exception ex)
            {
                Log.Error(string.Format( "An error occurred when adding subscriber to list. Please contact the system administration for more details", ex.Message), this);

                throw ex;
            }
        }
}

We can set the properties of the custom action by setting them in the Parameter field of the action definition item. Each property is set in an XML fragment with the element name matching the property name of the class. WFM will handle setting these properties of the class when it instantiates it.

  • Set value of the following fields to point to subscription action assembly

Field

Value

Assembly

Project.Business.dll

Class

Project.Business.SubscribeNewsletterUser 

  • To display subscription id dialog that prompts the users to enter subscripton id, I created SubscriptionIdEditor.xml that handles look and feel of my editor, and supporting class behind to display value of subscription id that previously saved when the editor is loaded, and to save and pass valueto SubscribeNewsletterUser class when OnOk button is clicked.

SubscriptionIdEditor.xml















SubscriptionIdEditor.cs

public class SubscriptionIdEditor: DialogForm
{
        protected XmlControl Dialog;
        protected NameValueCollection nvc = new NameValueCollection();

        public string Params
        {
            get
            {
                return (System.Web.HttpContext.Current.Session[Sitecore.Web.WebUtil.GetQueryString("params")] as string);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!Sitecore.Context.ClientPage.IsEvent)
            {
                nvc = ParametersUtil.XmlToNameValueCollection(Params);

                if (nvc.AllKeys.Length > 0)

                SubscriptionID.Value = nvc["SubscriptionID"];
            }
        }

        protected override void OnOK(object sender, EventArgs args)
        {
            // Get XML fragment of settings to store 
            nvc.Add("SubscriptionID", SubscriptionID.Value);

            string str = ParametersUtil.NameValueCollectionToXml(nvc);

            if (str.Length == 0)
            {
                str = "";
            }

            SheerResponse.SetDialogValue(str);

            base.OnOK(sender, args);
        }

        public Edit SubscriptionID{ get; set; }

    }

  •    To register this SubscriptionIdEditor control as the editor for the subscribe to newsletter action, add a reference to editor field of the action definition item. 

Field

Value

Editor

control:SubscriptionIdEditor

ValueEditorcontrol:Form.SelectItem

Share:

Archive

Syndication