Can a form be submitted in code?

Vote:
 

We are capturing form submissions and need to submit them on behalf of the user at a later time. Can it be submitted in code?

#308544
Sep 15, 2023 14:39
Vote:
 

Hi,

If I understand right you are collecting form data and then at a later point want to send this data to another service, then yes you can.

Have a look at the iformrepository and iformdatarepository interfaces.

Paul

#308546
Sep 15, 2023 17:56
Vote:
 

Yes u should be able to post the controller at a later time, you can use the network tab to look at the post and then post a form to the same location later.  You can also use the DataSubmissionService

using EPiServer.Core;
using EPiServer.Forms;
using EPiServer.Forms.Core.Internal;
using EPiServer.Forms.Core.Models.Internal;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Specialized;
using System.Linq;

namespace MyNamespace;

public class MyFormsSubmissionService
{
    private readonly DataSubmissionService _dataSubmissionService;
    private readonly IHttpContextAccessor _contextAccessor;
    private readonly IContentLanguageAccessor _contentLanguageAccessor;

    public MyFormsSubmissionService(DataSubmissionService dataSubmissionService,
        IHttpContextAccessor contextAccessor,
        IContentLanguageAccessor contentLanguageAccessor)
    {
        _dataSubmissionService = dataSubmissionService;
        _contextAccessor = contextAccessor;
        _contentLanguageAccessor = contentLanguageAccessor;
    }

    public SubmitActionResult Submit(Guid formId, NameValueCollection rawSubmittedData)
    {       
        if (formId == Guid.Empty)
        {
            return new SubmitActionResult
            {
                IsSuccess = false,
                RedirectUrl = _contextAccessor.HttpContext?.Request.GetTypedHeaders().Referer.AbsoluteUri ?? "",
                Message = "Form id is empty"
            };
        }

        if (!rawSubmittedData.AllKeys.Contains(Constants.FormGuidKey))
        {
            rawSubmittedData.Add(Constants.FormGuidKey, formId.ToString());
        }

        if (!rawSubmittedData.AllKeys.Contains(Constants.FormLanguage))
        {
            rawSubmittedData.Add(Constants.FormLanguage, _contentLanguageAccessor.Language.Name);
        }

        return _dataSubmissionService.PerformDataSubmit(rawSubmittedData, _contextAccessor.HttpContext);
    }
}
#308547
Edited, Sep 15, 2023 18:16
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.