0

I'm trying to export Umbraco Forms data to CSV, in the exact format generated when you export from the Entries table UI, and I'm trying to do this strictly from code. End result is I would like to run as a task to export data daily or every few hours, and save that .csv file to a folder on the server. This would be for bulk importing into other systems, and a Forms Workflow won't work in this case.

My original approach was to try and do this in SQL, but Forms data would be difficult to work with since it's a in a mix of JSON and native SQL data. I'm not running SQL Server 2016 so no JSON support.

I've been searching through the Forms API and found a few things that might help, but can't seem to find what I need. So far I've tried:

Umbraco.Forms.Data.Storage.RecordStorage.GetAllRecords()

This gets the records, but unsure where to get the field names. I don't even see them in the UF tables. I can generate JSON output via GenerateRecordDataAsJson() but only get Guids for field names.

I've tried looping through the above Record collection and manually working with individual items, and I've tried the RecordStorage.BuildRecord() method hoping that would assemble the data, but no luck there either.

trnelson
  • 2,715
  • 2
  • 24
  • 40

2 Answers2

1

You have to get the field names using the API. When Forms was still Contour, EVERYTHING was in the database, making it easy(ish) to query stuff like this.

I think you need to use FormStorage to get the details of the form and you should be able to get the fields from that.

Tim
  • 4,217
  • 1
  • 15
  • 21
0

Thought I'd share an example for Umbraco 8 and Forms 8.4.1 since I just ran into this same requirement recently.

public class ExportController : UmbracoApiController
{
    IRecordReaderService RecordReaderService;

    public ExportController(
        IRecordReaderService recordReaderService)
    {
        RecordReaderService = recordReaderService;
    }

    [HttpGet]
    public void Export()
    {
        var formId = new System.Guid("f9ea767a-0e4e-4c90-85f1-53ef42c60793");
        var pageSize = 50;
        var formResults = RecordReaderService.GetRecordsFromForm(formId, 1, pageSize);
        foreach (var entry in formResults.Items)
        {
            var firstName = entry.ValueAsString("firstName"); // Use field alias
            // Output data or other
        }
    }
}
Corgalore
  • 2,486
  • 2
  • 23
  • 32