I'm looking for a way to store a value from a field and display it somewhere else, on a different page for example.
As far as I understand they've changed a lot of these things in Umbraco 11.
I've been trying to use @using Umbraco.Forms.Data.Storage;
FormStorageand RecordStorage but it doesn't seem to work on Umbraco 11. I've also tried the request method and HttpContextAccessor.HttpContext.Request.Form.ContainsKey("myField"))
But none of them works. Any idea how to solve this? I'd be happy to share some of the code for y'all to get an idea on what I'm trying to achieve.
I've managed to store the different fields in the backoffice with Content Nodes.
Those content nodes are school classes, and I want the users to be able to toggle between the classes on the page itself. Preferably its bound to the member so they could swap between school classes they themselves registered. Apparently the UmbracoFormsStorage is no longer in the latest versions for some reason.
Anyone know how to do this in a Razor view in Umbraco 11? Or do I have to setup controllers and use the service APIs / GUID?
var currentMember = await MemberManager.GetCurrentMemberAsync();
if (currentMember != null)
{
var formId = "umbraco_form_2234732813ed4ada88a0370f2630e351"; // replace with your form ID
var storage = new UmbracoFormsStorage();
var form = storage.GetForm(Guid.Parse(formId));
var registeredClasses = form.AllFieldValues.ContainsKey("registeredClasses")
? form.AllFieldValues["registeredClasses"].ToString()
: "";
var classIds = registeredClasses.Split(',');
var classNodes = new List<IPublishedContent>();
foreach (var classId in classIds)
{
var classNode = Umbraco.Content(classId);
if (classNode != null)
{
classNodes.Add(classNode);
}
}
foreach (var classNode in classNodes)
{
<div>
<h3>@(classNode.Value<string>("klassensNamn"))</h3>
<p>@(classNode.Value<string>("aarskull"))</p>
<p>@(classNode.Value<string>("skolansNamn"))</p>
<p>@(classNode.Value<string>("skolansAdress"))</p>
<p>@(classNode.Value<string>("skolansPostnummer")) @(classNode.Value<string>("skolansPostort"))</p>
<p>@(classNode.Value<string>("antalElever"))</p>
<p>@(classNode.Value<string>("pedagogensNamn"))</p>
</div>
}
}
For Umbraco Forms on 10+ (maybe also 9, I haven't checked)...
Use Umbraco.Forms.Core.Services.IFormService and Umbraco.Forms.Core.Services.IRecordReaderService
With Dependency Injection:
using Umbraco.Forms.Core.Data.Storage;
using Umbraco.Forms.Core.Enums;
using Umbraco.Forms.Core.Interfaces;
using Umbraco.Forms.Core.Services;
public class MyService
{
private IFormService _FormService;
private IRecordReaderService _FormRecordReaderService;
public MyService(IFormService formService,IRecordReaderService formRecordReaderService)
{
_FormService = formService;
_FormRecordReaderService = formRecordReaderService;
}
public Form? GetForm(Guid FormGuid)
{
var form = _FormService.Get(FormGuid);
if (form != null)
{
return form;
}
else
{
var msg = $"Form with GUID '{FormGuid}' not found.";
return null;
}
}
public IEnumerable<IRecord> GetFormRecords(Form TheForm)
{
IEnumerable<IRecord> records = new List<IRecord>();
var allPagedRecs = _FormRecordReaderService.GetRecordsFromForm(TheForm.Id, 1, int.MaxValue);
if (allPagedRecs.Items != null)
{
records = allPagedRecs.Items.ToList();
var qtyApprovedRecords = records.Count(x => x.State == FormState.Approved);
}
return records;
}
}
Storing a field from a specific Umbraco Form
Hey,
I'm looking for a way to store a value from a field and display it somewhere else, on a different page for example.
As far as I understand they've changed a lot of these things in Umbraco 11.
I've been trying to use
@using Umbraco.Forms.Data.Storage; FormStorage
andRecordStorage
but it doesn't seem to work on Umbraco 11. I've also tried the request method andHttpContextAccessor.HttpContext.Request.Form.ContainsKey("myField"))
But none of them works. Any idea how to solve this? I'd be happy to share some of the code for y'all to get an idea on what I'm trying to achieve.
Cheers
Ad
I've managed to store the different fields in the backoffice with Content Nodes.
Those content nodes are school classes, and I want the users to be able to toggle between the classes on the page itself. Preferably its bound to the member so they could swap between school classes they themselves registered. Apparently the UmbracoFormsStorage is no longer in the latest versions for some reason.
Anyone know how to do this in a Razor view in Umbraco 11? Or do I have to setup controllers and use the service APIs / GUID?
@inherits UmbracoViewPage
@{ Layout = "USNMaster.cshtml"; Usnstyle websiteStyle = (Usnstyle)Model.WebsiteStyle;
}
For Umbraco Forms on 10+ (maybe also 9, I haven't checked)...
Use
Umbraco.Forms.Core.Services.IFormService
andUmbraco.Forms.Core.Services.IRecordReaderService
With Dependency Injection:
Hey,
Could you please elaborate that how to initiate IFormServices and IRecordServices?
They are provided via the Dependency Injection that Umbraco handles automatically. This code should "just work" in an Umbraco 9+ site.
is working on a reply...