6

We're using the same MVC form on multiple pages and want to capture the page URL that the form is completed on.

It's a custom 'Web Forms for Marketers' using WFFM placeholder, created via Sitecore itself. Ideally I want the page URL captured in an email notification as a starter, with future plan to pass this back to the database at some stage

  • How are you capturing the form submission? Is it an AJAX POST or an ASP.NET postback? Something else? Can you add some code samples to give more context? – Dan Sinclair Dec 14 '18 at 15:32
  • Is this a custom form? Sitecore Forms? WFFM? And what do you mean with "capture" - do you want it saved in the database or .. ? – Gatogordo Dec 14 '18 at 15:33
  • It's a custom 'Web Forms for Marketers' using WFFM placeholder, created via Sitecore itself. Ideally I want the page URL captured in an email notification as a starter, with future plan to pass this back to the database at some stage. – freddy Dec 14 '18 at 15:50
  • sitecore.stackexchange.com/questions/5881/… similar to your request. You could stick with this example and include the ContextItem.ID or simply include the request URL in the value instead. – jrap Dec 14 '18 at 16:29
7

It's not possible without some custom code. The simplest is to create a custom hidden field and store the page URL in this, which will then get submitted along with the other values of the form. You can then use this in your own save actions or the Send Email save action as part of the message body in much the same way as any other form value.

I have previously blogged about a custom Hidden Token Field in WFFM which does exactly this. The code can be shortened if you only want the URL.

Create the field and add properties to hide the inherited settings from the designer dialog:

public class HiddenUrl : InputControl
{
    protected HiddenUrl() {}
    protected HiddenUrl(HtmlTextWriterTag tag) : base(tag) {}

    // Override to hide fields in Form Editor
    public new string CssClass { get; set; }
    public new string Information { get; set; }
    public new string Text { get; set; }

    // If we were using ASP.Net WebForms then we'd need to expand this
    public virtual string Value { get; set; }
}

Create a model to be used for your MVC view:

public class HiddenUrlField : FieldViewModel, IFieldResult, IContainerMetadata
{
    public string ResultParameters { get; set; }

    [DefaultValue("")]
    public virtual string Value { get; set; }

    public virtual ControlResult GetResult()
    {
        return new ControlResult(this.FieldItemId, this.Name, (object)this.Value, this.ResultParameters, false);
    }

    public override void Initialize()
    {
        base.Initialize();

        var options = UrlOptions.DefaultOptions;
        options.AlwaysIncludeServerUrl = true;
        Value = LinkManager.GetItemUrl(Sitecore.Context.Item, options);
    }
}

And create the view for this field;

@model MyProject.CMS.Custom.WFFM.Fields.HiddenUrlField

@Html.HiddenFor(x => Model.FieldItemId)
@Html.HiddenFor(x => Model.Value)

We need to add this field to WFFM, create an item in Sitecore under /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types. For example:

  • Item Path: /sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/Custom/HiddenUrlField
  • Template: /sitecore/templates/Web Forms for Marketers/Field Type
  • Assembly: MyProject.CMS.Custom
  • Class: MyProject.CMS.Custom.WFFM.Fields.HiddenUrl
  • MVC Type: MyProject.CMS.Custom.WFFM.Fields.HuddenUrlField, MyProject.CMS.Custom

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.