Adding An Export Type To Umbraco Forms
This builds on the "adding a type to the provider model" chapter.
Add a new class to your project and have it inherit from Umbraco.Forms.Core.ExportType. You have two options when implementing the class, as shown in the following examples.
Basic Example
You can implement the method public override Task<string> ExportRecordsAsync(Guid formId, RecordExportFilter filter) in your export provider class. You need to return the string you wish to write to a file. For example, you can generate a .csv (comma-separated values) file. You would implement your logic to build up a comma-separated string in the ExportRecordsAsync method.
FileExtension is the extension such as zip, txt or csv of the file you will be generating and serving from the file system.
In this example below we will create a single HTML file which takes all the submissions/entries to be displayed as a HTML report. We will do this in conjunction with a Razor partial view to help build up our HTML and thus merge it with the form submission data to generate a string of HTML.
Provider Class
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Umbraco.Extensions;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Models;
using Umbraco.Forms.Core.Searchers;
using Umbraco.Forms.Web.Helpers;
namespace MyFormsExtensions
{
public class ExportToHtml : ExportType
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IFormRecordSearcher _formRecordSearcher;
public ExportToHtml(
IHttpContextAccessor httpContextAccessor,
IFormRecordSearcher formRecordSearcher)
{
_httpContextAccessor = httpContextAccessor;
_formRecordSearcher = formRecordSearcher;
Name = "Export as HTML";
Description = "Export entries as a single HTML report";
Alias = "exportAsHtml";
Id = new Guid("4117D352-FB41-4A4C-96F5-F6EF35B384D2");
FileExtension = "html";
Icon = "icon-article";
}
public override async Task<string> ExportRecordsAsync(Guid formId, RecordExportFilter filter)
{
var view = "~/Views/Partials/Forms/Export/html-report.cshtml";
EntrySearchResultCollection model = _formRecordSearcher.QueryDataBase(filter);
return await ViewHelper.RenderPartialViewToString(_httpContextAccessor.GetRequiredHttpContext(), view, model);
}
}
}Razor Partial View
Registration
Advanced Example
This approach gives us more flexibility in creating the file we wish to serve as the exported file. We do this for the export to Excel file export provider we ship in Umbraco Forms. With this we can use a library to create the Excel file and store it in a temporary location before we send back the filepath for the browser to stream down the export file.
In this example we will create a collection of text files, one for each submission which is then zipped up into a single file and served as the export file.
Date values
The CSV and Excel export types both inherit from CsvExportTypeBase. A date value is written using the culture the entry was submitted with. The writer's own culture stays invariant, so delimiters and number formats do not vary between entries.
To control the date format yourself, register a DateTime converter by overriding ConfigureContext. Forms then leaves every date value to your converter:
To change the writer itself, such as the delimiter it uses, override GetWriterConfiguration.
Localization
The backoffice uses localization keys to display the label and description for each export type. These keys are based on the Alias property set in the constructor:
Label:
formProviderExportTypes_{alias}Description:
formProviderExportTypes_{alias}Description
For example, an export type with Alias = "exportAsHtml" will look for the keys formProviderExportTypes_exportAsHtml and formProviderExportTypes_exportAsHtmlDescription.
Create a JavaScript language file containing the translations:
Register the language file with a localization manifest:
Register the localization manifests in your entry point:
For more details on setting up localization files, see the Localization article.
Last updated
Was this helpful?