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.
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
Hi Romeo,
You'll want something like this:
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
Hi Tim. This is great. thanks.
I have another question though, what are my options if I don't have the formId and fieldID?
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
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);
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
Yeah. I figured I'd have to query the database for this one. Thanks for all the reply. I appreciate the help.
Thanks,Romeo
is working on a reply...