2

Sitecore 8.2 rev.170407, WFFM v8.2 rev.161129

I am working on creating custom send email save action which should update some email body and do some extra logic before sending an email. To modify the content of email I need to get Sitecore.Context.Item or Sitecore.Context.Item.Id but Sitecore.Context.Item is null.

Here the code example:

public class SendCustomMessage : SendMessage
 {
    public SendCustomMessage(ISettings settings, IMailSender mailSender) : base(settings, mailSender)
    {
    }

    public override void Execute(ID formId, AdaptedResultList adaptedFields, ActionCallContext actionCallContext = null, params object[] data)
    {
         ProcessMessageArgs processMessageArgs = new ProcessMessageArgs(formId, adaptedFields, this.MessageType, null);
         //update email body based on Sitecore.Context.Item and put everything to processMessageArgs 
         CorePipeline.Run("processMessage", (PipelineArgs)processMessageArgs);
    }
}

I found out the post about Hidden Item Id field here.

But is it possible to get sitecore context item id without adding Hidden Id field?

  • Can I ask why you need the Context Item? You just need the email body from the form? The Context Item is null because you are in a pipeline. – Chris Auer May 26 '17 at 1:51
3

I cleaned up the link contained in the question because I found it overly complex for what it needs to accomplish.

HiddenIdField.cs

[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public class HiddenIdField : SingleLineText
{
    protected override void OnLoad(System.EventArgs e)
    {
        Text = Sitecore.Context.Item.ID.ToString();

        Attributes["style"] = "display: none";
    }
}

HiddenIdFieldViewModel.cs

public class HiddenIdFieldViewModel : Sitecore.Forms.Mvc.ViewModels.Fields.SingleLineTextField
{
    /// <summary>
    /// Initializes field
    /// </summary>
    public override void Initialize()
    {
        if (!string.IsNullOrWhiteSpace(CssClass))
        {
            CssClass += " hidden";
        }

        else
        {
            CssClass = "hidden";
        }

        ShowTitle = false;

        Value = Sitecore.Context.Item.ID.ToString();
    }
}

From link:

The last part is to create (a) Sitecore item. It should be added below /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types (in master database). This item should be based on (the) template /sitecore/templates/Web Forms for Marketers/Field Type and populated with:

  • Assembly - file name of code assembly
  • Class - class name of field type with full namespace
  • MVC Type - class name of field type view model with full namespace and its assembly name (after comma)

Note: I also had to add the following to CSS

[data-wffm] > .hidden{
  display: none;
}
  • do not want that client care about additional field in form which do not have any business value – Vlad Oct 7 '17 at 13:09
  • I understand the question but at this point there is no other solution present. I wrote this out to aid other whom may be asking the same question but fthey nd his acceptable – jrap Oct 7 '17 at 14:22
1

In short: No.

Sitecore.Context gets built during a regular page request by the httpRequestBegin pipeline. This pipeline gets bypassed when submitting a form generated via WFFM for a lot different reasons.

You could probably extend and override base WFFM processors and pipeline steps in some way to achieve it; but it would be more invasive than just going with the clean solution you already have.

1

Due to the possiblity of Save Actions executing out of context, via the Event Queue and on the CM server, normally we use also use a hidden field as well. Any logic is then based off of that GUID.

If you really want to access the Context Item from the Save Action then it is possible. You need to ensure that your form is not an AJAX form. You can only set this field on your form via the Cotent Editor:

Is Ajax Mvc Form Field

And also verify that your Save Action is marked as a Client Action. Again, you must do this from the Content Editor:

Save Action

The combination of both these settings ensures that the Save Action runs under the context of the current Item and on the CD server.

You can then access Sitecore.Context.Item as normal from the Execute() method of the Save Action or from processors in the processMessage pipeline. You need to ensure you defensively code around the Item being null, esp if the processor is also deployed to the CM server and remote actions are enabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.