IDocumentExporter

The IDocumentExporter interface allows you to implement document exporters to export data from the document system.  When document exporters are registered, a "Export" button will be shown in the document system backend within each model.  When the button is clicked, there will be a menu item for each registered exporter.   The document system includes a couple of exporters, the JsonExporter and the XmlExporter.  By adding the Lisa.Modules.CvsHelper and Lisa.Mofules.Office.EPPlus modules, additional exporters will become available.  The TextDelimitedExporter and ExcelExporter respectively.

If you need to be able to export data from the document system in your own format, you can simply implement your own document exporter.

Interface declaration

public interface IDocumentExporter
{
	string MimeType { get; }
	string FileName { get; }

	Task<byte[]> GetData(Guid modelId, QueryFilter filters, IEnumerable<string> tags, Guid? folderId);
}

Example

This example shows how the standard JsonExporter is implemented.

[DisplayName("JSON")]
[Description("Exports the data as a JSON text file")]
public class JsonExporter : IDocumentExporter
{
	private readonly IDocumentQueryService _documentQueryService;
	private readonly IModelRepository _modelRepository;

	public string MimeType => "application/json";

	public string FileName => "{0}-JsonExport-{1}.json";

	[EditorBrowsable]
	[DisplayName("Only field values")]
	[Description("If this is checked, only the user defined fields are returned.")]
	public bool OnlyFieldValues { get; set; }

	public JsonExporter(IDocumentQueryService documentQueryService, IModelRepository modelRepository)
	{
		_documentQueryService = documentQueryService;
		_modelRepository = modelRepository;
	}

	public async Task<byte[]> GetData(Guid modelId, QueryFilter filters, IEnumerable<string> tags, Guid? folderId)
	{
		TagFilter tagFilter = null;
		if (tags != null && tags.Any())
		{
			tagFilter = new TagFilter();
			tagFilter.Tags = tags.ToList();
			tagFilter.FilterType = TagFilterType.Any;
		}

		var documents = await _documentQueryService.GetDocuments(modelId, null, null, folderId.HasValue ? new List<Guid>() { folderId.Value } : null, filters, tagFilter, false);
		var model = _modelRepository.FindById(modelId);

		// Add the first class properties to the FieldValues so they will also be returned
		if (!OnlyFieldValues)
		{
			documents.Documents.ToList().ForEach(x =>
			{
				x.FieldValues.Add("ID", x.Id);
				x.FieldValues.Add("Created", x.Created);
				x.FieldValues.Add("Modified", x.Modified);
				x.FieldValues.Add("OwnerId", x.OwnerId);
				x.FieldValues.Add("OwnerName", x.OwnerName);
				x.FieldValues.Add("Tags", x.Tags);

				if (model.EnableDateRestrictions)
				{
					x.FieldValues.Add("DisplayFrom", x.DisplayFrom);
					x.FieldValues.Add("DisplayTo", x.DisplayTo);
				}
			});
		}

		var data = documents.Documents.Select(x => x.FieldValues);

		var json = JsonConvert.SerializeObject(data);
		return Encoding.UTF8.GetBytes(json);
	}
}

Registration

This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).

public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
	services.AddNamed<IDocumentExporter, JsonExporter>(ServiceLifetime.Transient);
}