Copied to clipboard

Flag this post as spam?

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


  • Romeo Cadaoas 33 posts 110 karma points
    May 15, 2017 @ 07:20
    Romeo Cadaoas
    1

    Easiest way to get a record by field value

    I have a field called "Order Code" and I want to get my Umbraco.Forms.Core.Record by querying the value of this field.

    What's the most efficient way to do this using Umbraco form functions?

    -Romeo

  • Tim Watts 90 posts 395 karma points
    May 15, 2017 @ 09:12
    Tim Watts
    101

    Hi Romeo,

    You'll want something like this:

        private IEnumerable<Record> GetEntries(Guid formId, Guid fieldId, string requiredValue)
        {
            using (var formStorage = new FormStorage())
            {
                using (var recordStorage = new RecordStorage())
                {
                    Form form = formStorage.GetForm(formId);
                    // get all records (entries) for that form
                    IEnumerable<Record> records = recordStorage.GetAllRecords(form);
    
                    return records.Where(x => x.RecordFields[fieldId].ValuesAsString() == requiredValue);
                }
            }
        }
    

    For this you'll need the Guid for the form, and for the field which you wish to query. You'll also need usings for Umbraco.Forms.Core and Umbraco.Forms.Data.Storage.

    Regards,

    Tim

  • Romeo Cadaoas 33 posts 110 karma points
    May 15, 2017 @ 09:36
    Romeo Cadaoas
    0

    Hi Tim. This is great. thanks.

    I have another question though, what are my options if I don't have the formId and fieldID?

  • Tim Watts 90 posts 395 karma points
    May 15, 2017 @ 09:58
    Tim Watts
    0

    formStorage.GetForm will also work with the form name

    RecordFields is just a standard dictionary of

    Just remember that the Form Name and the Question Caption can easily be changed and this will break your code.

    Tim

  • Romeo Cadaoas 33 posts 110 karma points
    May 15, 2017 @ 11:28
    Romeo Cadaoas
    0

    Might be pushing it too much but is it possible to get all records without passing the form e.g. recordStorage.GetAllRecords()

    because all I want to use is this one

    records.Where(x => x.RecordFields[fieldId].ValuesAsString() == requiredValue);

  • Tim Watts 90 posts 395 karma points
    May 15, 2017 @ 11:52
    Tim Watts
    0

    As far as I'm aware everything needs to come through the form.

    Unless of course you want to try to get the data directly from the SQL tables (all named starting dbo.UFRecord...)

    Tim

  • Romeo Cadaoas 33 posts 110 karma points
    May 15, 2017 @ 13:00
    Romeo Cadaoas
    0

    Yeah. I figured I'd have to query the database for this one. Thanks for all the reply. I appreciate the help.

    Thanks,Romeo

Please Sign in or register to post replies

Write your reply to:

Draft