Created
April 12, 2023 12:12
-
-
Save AndyButland/3463cc414f405fdbef556465017dc456 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc; | |
using Umbraco.Cms.Web.Common.Controllers; | |
using Umbraco.Forms.Core.Models; | |
using Umbraco.Forms.Core.Persistence.Dtos; | |
using Umbraco.Forms.Core.Services; | |
namespace Umbraco.Forms.Testsite.Controllers | |
{ | |
public class SubmitFormTestController : UmbracoApiController | |
{ | |
private readonly IRecordService _recordService; | |
private readonly IFormService _formService; | |
public SubmitFormTestController(IRecordService recordService, IFormService formService) | |
{ | |
_recordService = recordService; | |
_formService = formService; | |
} | |
public IActionResult SubmitForm() | |
{ | |
var submittedValues = new Dictionary<string, string>() | |
{ | |
{ "name", "Andy 2" } | |
}; | |
// Form has single field with alias of "name". | |
var formId = Guid.Parse("be745434-2607-40cb-84be-db13b9f1dc43"); | |
Form? form = _formService.Get(formId); | |
if (form == null) | |
{ | |
throw new InvalidOperationException($"Could not find form with ID: {formId}"); | |
} | |
var record = new Record | |
{ | |
Created = DateTime.Now, | |
Form = form.Id, | |
Culture = Thread.CurrentThread.CurrentCulture.Name, | |
}; | |
foreach (Field field in form.AllFields) | |
{ | |
var recordField = new RecordField(field) | |
{ | |
Alias = field.Alias, | |
FieldId = field.Id, | |
Key = Guid.NewGuid(), | |
Record = record.Id, | |
Values = new List<object>() { submittedValues[field.Alias] } | |
}; | |
record.RecordFields.Add(field.Id, recordField); | |
} | |
_recordService.Submit(record, form); | |
return Ok("Done"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment