diff --git a/13/umbraco-forms/developer/working-with-data.md b/13/umbraco-forms/developer/working-with-data.md index efc171cb19e..9828140c772 100644 --- a/13/umbraco-forms/developer/working-with-data.md +++ b/13/umbraco-forms/developer/working-with-data.md @@ -110,3 +110,45 @@ Sample script that is outputting comments using a Form created with the default } ``` + + +## Loading a Record From a Submitted Form +When a form is submitted, the submitted form ID and the saved record ID are stored in the `TempData` so they can be referenced. + +You can use the FormService and the RecordStorage to get the `Form` and `Record` objects. + +Here is a sample code for retrieving a record in a view. + +``` +@using Umbraco.Forms.Core.Models +@using Umbraco.Forms.Core.Persistence.Dtos +@using Umbraco.Forms.Core.Data.Storage +@using Umbraco.Forms.Core.Services +@inject IFormService _formService +@inject IRecordStorage _recordStorage +@inherits UmbracoViewPage +@{ + Guid formId; + Form? form; + Guid recordId; + Record? record; + string submittedEmail; + + if (Guid.TryParse(TempData["UmbracoFormSubmitted"]?.ToString(), out Guid formId) && + Guid.TryParse(TempData["Forms_Current_Record_id"]?.ToString(), out Guid recordId)) + { + + form = _formService.Get(formId); + + if (form != null && TempData["Forms_Current_Record_id"] != null) + { + Guid.TryParse(TempData["Forms_Current_Record_id"]?.ToString(), out recordId); + + record = _recordStorage.GetRecordByUniqueId(recordId, form); + + submittedEmail = record.GetRecordFieldByAlias("email")?.ValuesAsString(); + } + } +} +``` + diff --git a/16/umbraco-forms/developer/working-with-data.md b/16/umbraco-forms/developer/working-with-data.md index eb4ef07555a..b5d42a577bf 100644 --- a/16/umbraco-forms/developer/working-with-data.md +++ b/16/umbraco-forms/developer/working-with-data.md @@ -110,3 +110,43 @@ Sample script that is outputting comments using a Form created with the default } ``` + +## Loading a Record From a Submitted Form +When a form is submitted, the submitted form ID and the saved record ID are stored in the `TempData` so they can be referenced. + +You can use the FormService and the RecordStorage to get the `Form` and `Record` objects. + +Here is a sample code for retrieving a record in a view. + +``` +@using Umbraco.Forms.Core.Models +@using Umbraco.Forms.Core.Persistence.Dtos +@using Umbraco.Forms.Core.Data.Storage +@using Umbraco.Forms.Core.Services +@inject IFormService _formService +@inject IRecordStorage _recordStorage +@inherits UmbracoViewPage +@{ + Guid formId; + Form? form; + Guid recordId; + Record? record; + string submittedEmail; + + if (Guid.TryParse(TempData["UmbracoFormSubmitted"]?.ToString(), out Guid formId) && + Guid.TryParse(TempData["Forms_Current_Record_id"]?.ToString(), out Guid recordId)) + { + + form = _formService.Get(formId); + + if (form != null && TempData["Forms_Current_Record_id"] != null) + { + Guid.TryParse(TempData["Forms_Current_Record_id"]?.ToString(), out recordId); + + record = _recordStorage.GetRecordByUniqueId(recordId, form); + + submittedEmail = record.GetRecordFieldByAlias("email")?.ValuesAsString(); + } + } +} +```