Skip to main content

(20) CSV Import

- Support Ticket Tutorial -

Moving Data into the App

This tutorial will enable the user to import the title of a Ticket, a Description, and the Ticket Severity from a CSV file. The data for those attributes will be added to the Ticket entity and displayed.

1. Install CsvHelper

  • Go to Config > Resources.
  • Click Add resource > Add NuGet package assembly resource.
  • Enter CsvHelper and add it to your app.

2. Establish CSVImporter

  • Go to Code > Custom Code.
  • Click (+)
  • Set Name of class or interface to: CsvImporter.
  • Click Finish
  • Copy and paste the following code:
Click to expand the code block
public class CsvImporter
{
public static IEnumerable<T> ImportData<T>(IDocument document, Encoding encoding = null, CsvHelper.Configuration.ClassMap classMap = null)
{
if (!document.HasAnyRevision())
{
return Enumerable.Empty<T>();
}

using(var stream = new MemoryStream(document.GetLatestRevision().Content))
using(var reader = new StreamReader(stream, encoding ?? Encoding.UTF8))
using(var csvHelper = new CsvHelper.CsvReader(reader, System.Globalization.CultureInfo.GetCultureInfo("en-US")))
{
if (classMap != null)
{
csvHelper.Context.RegisterClassMap(classMap);
}

return csvHelper.GetRecords<T>().ToList();
}
}
}

3. Establish TicketImporter

This step will take data from a three-column csv spreadsheet. Each row will represent a new record for the Ticket Entity.

  • Create another Custom Code (on Code > Custom Code). Name the new file: TicketImporter.
  • Copy and paste the following code:
Click to expand the code block
public class TicketImporter
{
[CsvHelper.Configuration.Attributes.Index(0)]
public string Title
{
get;
set;
}

[CsvHelper.Configuration.Attributes.Index(1)]
public string Description
{
get;
set;
}

[CsvHelper.Configuration.Attributes.Index(2)]
public string TicketSeverity
{
get;
set;
}

}

4. Create a Custom Command

A Custom Command will need to be established to import the csv file into the app.

  • Go to Business > Commands
  • Click (+)
  • Set Name to: Import
  • Verify that Type is set to Custom Command
  • Verify that Result is set to None
  • Click Finish

There are four tabs on the Commands page.

  • Go to the Model tab.
  • In the Attributes section, click Add Attribute.
  • Set Name to CSV Sheet
  • Set Type to: Document
  • Click Save

  • Go to the View tab.
  • In the Data Fields section, select CSVSheet (Document) to be placed on the Page Layout

  • Go to the Code tab.
  • Copy and paste the following:

Click to expand the code block

Click to expand the code block
(model, db, ctx) =>
{
var numberFormat = new System.Globalization.NumberFormatInfo() { NumberDecimalSeparator = "." };
var
import = CsvImporter.ImportData<TicketImporter>(model.CSVSheet);

// this will serve as a row counter during import
var row = 0;

foreach(var i in
import)
{
row++;

db.TicketSet.Add(new Ticket()
{
Title = i.Title,
Description = i.Description,
CreatedBy = ctx.User,
CreatedDate = DateTime.Now,
TicketSeverity = TicketSeverity.AllEntries.Single(s => s.Code == i.TicketSeverity.Trim())
});

}
}

5. Establish a Connection to the Command

The User Interface needs access to the upload feature. This can be through an Action Button on the Dashboard or a Detail Page.

  • Go to UI > Dashboard.
  • Double-click Support Ticket Dashboard
  • Click (+) Add Action
  • Set Type to: Link Custom Command Page
  • Set Custom Command to: Import
  • Change Button Label to: CSV Import
  • Click OK

6. Import Data