Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Adrian 5 posts 75 karma points
    Mar 20, 2023 @ 12:32
    Adrian
    0

    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; 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.

    Cheers

    Ad

  • Adrian 5 posts 75 karma points
    Apr 03, 2023 @ 11:31
    Adrian
    0

    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?

  • Adrian 5 posts 75 karma points
    Apr 03, 2023 @ 14:49
    Adrian
    0

    @inherits UmbracoViewPage

    @{ Layout = "USNMaster.cshtml"; Usnstyle websiteStyle = (Usnstyle)Model.WebsiteStyle;

    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>
        }
    }
    

    }

  • Heather Floyd 603 posts 1001 karma points MVP 5x c-trib
    May 17, 2023 @ 22:26
    Heather Floyd
    1

    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;
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft