1

Is there a way to programmatically access the submitted forms response of the user?

Scenario: User A submits a Sitecore Form and is redirected to a page where he can see all his answers to the form.

1
  • 2
    welcome to sse!. You would need a custom submit action to store the User responses/answers in the form to a ViewBag/ViewData. It could be used to display the data on submission
    – Abhay Dhar
    Apr 20 at 5:14
1

If you are using the Redirect to Page action you won't be able to access the submitted data. One solution would need to create a custom action that will save the form's values in Session/Cache/TempData and would be executed before the Redirect to Page one.

You can find more details on how to implement a custom action in the sitecore documentation https://doc.sitecore.com/developers/93/sitecore-experience-manager/en/walkthrough--creating-a-custom-submit-action.html. Your submit button would look like this: enter image description here

Another solution would be to have only one custom action that would be reusing the Redirect Page implementation:

public class CustomRedirectToPage : SubmitActionBase<RedirectActionData>
{
    public CustomRedirectToPage(ISubmitActionData submitActionData) : base(submitActionData)
    {
    }

    protected override bool Execute(RedirectActionData data, FormSubmitContext formSubmitContext)
    {
        Assert.ArgumentNotNull((object)formSubmitContext, nameof(formSubmitContext));
        if (data == null || !(data.ReferenceId != Guid.Empty))
            return false;
        Item obj = Context.Database.GetItem(new ID(data.ReferenceId));
        if (obj == null)
            return false;

        foreach (var viewModel in formSubmitContext.Fields)
        {
            var type = viewModel.GetType();
            var field = (InputViewModel<string>)viewModel;
            var value = field.Value;

           //store the field values
        }

        ItemUrlBuilderOptions urlBuilderOptions = this.LinkManager.GetDefaultUrlBuilderOptions();
        urlBuilderOptions.SiteResolving = new bool?(Settings.Rendering.SiteResolving);
        formSubmitContext.RedirectUrl = new UrlString(this.LinkManager.GetItemUrl(obj, urlBuilderOptions)).ToString();
        formSubmitContext.RedirectOnSuccess = true;
        formSubmitContext.Abort();
        return true;
    }
}

Note that the above code only reads the simple string fields types, for the others you can use a more generic method similar to the one described https://madhuanbalagan.com/generic-method-to-read-values-from-any-type-of-form-fields

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.