Working With Record Data
Developer documentation on working with Forms record data.
Umbraco Forms includes some helper methods that return records of a given Form, which can be used to output records in your templates using razor.
Available Methods
The methods can be found by injecting the Umbraco.Forms.Core.Services.IRecordReaderService interface. These methods are paged, but each one returns the records for an entire Form or Umbraco page. To filter records on the server, use the IFormRecordSearcher interface instead. It is described in the Querying records with a filter section below, and supports options such as a submission date range.
GetApprovedRecordsFromPage
PagedModel<Record> GetApprovedRecordsFromPage(int pageId, int pageNumber, int pageSize)Returns all records with the state set to approved from all Forms on the Umbraco page with the id = pageId .
GetApprovedRecordsFromFormOnPage
PagedModel<Record> GetApprovedRecordsFromFormOnPage(int pageId, Guid formId, int pageNumber, int pageSize)Returns all records with the state set to approved from the Form with the id = formId on the Umbraco page with the id = pageId as a PagedModel<Record>.
GetApprovedRecordsFromForm
PagedModel<Record> GetApprovedRecordsFromForm(Guid formId, int pageNumber, int pageSize)Returns all records with the state set to approved from the Form with the ID = formId as a PagedModel<Record>.
GetRecordsFromPage
PagedModel<Record> GetRecordsFromPage(int pageId, int pageNumber, int pageSize)Returns all records from all Forms on the Umbraco page with the id = pageId as a PagedModel<Record>.
GetRecordsFromFormOnPage
Returns all records from the Form with the id = formId on the Umbraco page with the id = pageId as a PagedModel<Record>.
GetRecordsFromForm
Returns all records from the Form with the ID = formId as a PagedModel<Record>.
The returned objects
All of these methods will return an object of type PagedModel<Record> so you can iterate through the Record objects.
The properties available on a Record are:
UmbracoPageKey is the GUID identifier of the page a form was submitted from. It is the preferred reference over the integer UmbracoPageId.
In order to access custom Form fields, these are available in the RecordFields property. Furthermore there exists an extension method named ValueAsString on Record in Umbraco.Forms.Core.Extensions, such that you can get the value as string given the alias of the field.
This extension method handle multi value fields by comma separating the values. E.g. "A, B, C"
Sample razor script
Sample script that is outputting comments using a Form created with the default comment Form template.
Querying records with a filter
The IRecordReaderService methods above return all records for a Form or Umbraco page. When you need to query records with server-side filtering, for example, retrieving only the submissions created within a date range, inject the Umbraco.Forms.Core.Searchers.IFormRecordSearcher interface instead.
This is the same database-level query used by the entries view in the backoffice. The filtering and paging are applied in the query sent to the database, so it does not load every record for the Form into memory.
Describe the query by passing a RecordFilter. Its main properties are:
StartDate and EndDate filter on the record's created date and are matched in UTC, so provide UTC values. Both dates are optional. If you leave StartDate unset, it defaults to a date far in the past. If you leave EndDate unset, it defaults to the current time. As a result, you can filter by only a start date or only an end date.
The following example retrieves the records for a Form created in the last 24 hours:
Each EntrySearchResult exposes the record metadata (Id, UniqueId, State, Created, Updated, and so on) together with a Fields collection. To read the value of a specific field, match the field's Alias in the returned Schema to its Id, then find the matching entry in Fields:
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.
Last updated
Was this helpful?