sitecorepipelinesitecore7web-forms-for-marketerssitecore7.5

How can I pass some information in to a Sitecore Webforms for Marketers custom message processor pipeline?


I have a site built using Sitecore 7.5 and Webforms for Marketers 2.5. I am trying to create a custom email message processor pipeline command that will change the TO field for the email before it goes out. But the proper email address needs to come from a Session variable.

Here is my class:

public class CustomEmailMessageProcessor
{
    public void Process(ProcessMessageArgs args)
    {
        //Change the TO address for the email based on the selection in the Subject field
        var subjectField = args.Fields.GetEntryByName("Subject");
        if (subjectField == null)
        {
            return;
        }

        //The value of the selected item will be the ID of a Subject Option
        var selectedSubjectOptionItem = Sitecore.Context.Database.GetItem(new ID(subjectField.Value));
        if (selectedSubjectOptionItem == null)
        {
            return;
        }

        var selectedSubjectOption = selectedSubjectOptionItem.GlassCast<Contact_Us_Subject_Option>();

    //Based on the currently selected Region in the Session variable, get the proper
    //child of selectedSubjectOption to populate the TO field
        ???

        if (args.To.Length != 0)
        {
            args.To.Append(",");
        }
        args.To.Append(proper email address goes here);
    }
}

And here is my associated config:

  <processMessage>
    <processor type="Sitecore.Form.Core.Pipelines.ProcessMessage.ProcessMessage, Sitecore.Forms.Core" method="ExpandLinks" />
    <processor type="Sitecore.Form.Core.Pipelines.ProcessMessage.ProcessMessage, Sitecore.Forms.Core" method="ExpandTokens" />
    <processor type="Sitecore.Form.Core.Pipelines.ProcessMessage.ProcessMessage, Sitecore.Forms.Core" method="AddHostToItemLink" />
    <processor type="Sitecore.Form.Core.Pipelines.ProcessMessage.ProcessMessage, Sitecore.Forms.Core" method="AddHostToMediaItem" />
    <processor type="Sitecore.Form.Core.Pipelines.ProcessMessage.ProcessMessage, Sitecore.Forms.Core" method="AddAttachments" />
    <processor type="Sitecore.Form.Core.Pipelines.ProcessMessage.ProcessMessage, Sitecore.Forms.Core" method="BuildToFromRecipient" />
    <!-- Custom setting -->
    <processor type="myProject.CustomEmailMessageProcessor, myProject" method="Process" />
    <processor type="Sitecore.Form.Core.Pipelines.ProcessMessage.ProcessMessage, Sitecore.Forms.Core" method="SendEmail" />
  </processMessage>

The user will select a Subject from a dropdown list on the form. The value of each Subject will be the Guid of a corresponding item. That subject item will have children. Each child will have an email address and will correspond to a Region item in the system. I will then find the child item that matches the Region ID that is stored in Session. Then I will have the correct email address to send the email to.

However I have no idea how to access the Session variable from within the pipeline (or if it is even possible). It doesn't have to be Session. I am happy to pass in the currently selected Region in some other fashion. I just need some way to pass information in that can be accessed from the pipeline code.

Thanks, Corey


Solution

  • When using the Sitecore WFFM webservice, the save action is run on the CMS, shell site. The save action don't know anything about the user session. A different more standard solution is create a (custom) Field hidden, and set there the value you can use to find the needed email address. Create a custom save action and do there your e-mail sending.

    Note: If you do not have a good identifier to find the email address watch out with putting an email address or to simple identifier in a (hidden) form value, it can be abused.

    Example of a hidden field, based on a <input type="text" .....> for Webforms

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Sitecore.Form.Web.UI.Controls;
    
    namespace StockpickSitecore.Controls.WFFM.CustomFields
    {
        public class HiddenEmailfield : SingleLineText
        {
            private static readonly string baseCssClassName = "scfSingleLineTextBorder";
            private string CssClassName { get; set; }
    
            public int MaxLength
            {
              get
              {
                return this.textbox.MaxLength;
              }
              set
              {
                this.textbox.MaxLength = value;
              }
            }
    
            public int MinLength { get; set; }
    
            public new string CssClass
            {
              get
              {
                return base.CssClass;
              }
              set
              {
                base.CssClass = value;
              }
            }
    
            public HiddenEmailfield()
            {
                this.Text = "info@yourdomein.com";
            }
    
            protected override void OnInit(EventArgs e)
            {
                this.Attributes.Add("style", "display: none;");
              this.textbox.CssClass = "scfSingleLineTextBox";
              this.help.CssClass = "scfSingleLineTextUsefulInfo";
              this.generalPanel.CssClass = "scfSingleLineGeneralPanel";
              this.title.CssClass = "scfSingleLineTextLabel";
              this.textbox.TextMode = TextBoxMode.SingleLine;
              this.Controls.AddAt(0, (Control) this.generalPanel);
              this.Controls.AddAt(0, (Control) this.title);
              this.generalPanel.Controls.AddAt(0, (Control) this.help);
              this.generalPanel.Controls.AddAt(0, (Control) this.textbox);
            }
        }
    }
    

    Change the constructor HiddenEmailfield and set the this.Text with the Session value.

    Creat a item in Sitecore below /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/Custom/ based on Template: /sitecore/templates/Web Forms for Marketers/Field Type

    Fill in the Assembly : StockpickSitecore for Example

    Fill in the Class: StockpickSitecore.Controls.WFFM.CustomFields.HiddenEmailfield

    Now you can use HiddenEmailfield in you webform als field type