Plugin: CSV
Introduction
The CSV plugin lets you export the raw data from the Casewhere system to the CSV file using the CsvHelper library.
Features
Export a CSV file
From version 1.2.0, the method Export is replaced by WriteData. There are 2 mandatory paramaters, FilePath and Data. Optional parameters are described in the next section.
Method Name: WriteData
Input
- FilePath: Required
- Data: Required, it's must be
IEnumerable<IEnumerable<object>>
- Append: Boolean value which indicates the file should be appended or overwritten
- IncludeHeaders: Boolean value which indicates whether the first line in the input data. If it is true, the first line is treated as headers
- Options:
IDictionary<string, object>
. The options are the same with the plugin's settings. If an option is defined here, it will overwrite the corresponding plugin's setting
Following the below sample:
var options = new Dictionary<string, object>
{
{ "CultureInfo", "da-DK" },
{ "EscapeCharacter", '"' },
{ "Delimiter", "," },
{ "DateTimeFormat", "dd-MMM-yyyy HH:mm:ss" },
{ "EncloseAll", false },
{ "EnclosingCharacter", '"' },
{ "Encoding", "UTF-8" },
{ "ByteOrderMark", false },
{ "BigEndian", false },
{ "EncloseAll", false }
};
var parameters = new Dictionary<string, object>
{
{ "FilePath" , "\Test\output.csv" },
{ "Data" , data },
{ "Append", true },
{ "IncludeHeaders", true },
{ "Options", options }
};
Use the Invoke method in IPluginApi to call WriteData feature:
IPluginApi.Invoke("CwCsv", "WriteData", IDictionary<string, object> parameters);
- Example:
var path = Path.GetTempPath();
var fileName = $"cw_csv_export_sample_{DateTime.Now.ToString("ddMMyyyyHHmmss")}.csv";
var filePath = Path.Combine(path, fileName);
DateTime rst = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Romance Standard Time");
DateTime cst = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "China Standard Time");
DateTime utc = DateTime.UtcNow;
var data = new List<List<object>>
{
new List<object> { "Name", "Age", "BirthDate" }
new List<object> { "Romance Standard Time", 18, rst }
new List<object> { "China Standard Time", 19, cst }
new List<object> { "Current Time", 25, DateTime.Now }
new List<object> { "UTC én af de påkrævede", 25, DateTime.Now }
}
var parameters = new Dictionary<string, object>
{
{ "FilePath" , filePath },
{ "Data" , data },
{ "Append", false },
{ "IncludeHeaders", true }
};
var pluginResult = pluginApi.Invoke("CwCsv", "WriteData", parameters);
var result = pluginResult.Data;
bool isSuccess = result["IsSuccess"];
string errorMessage = result["Error"];
Method Name: [Obsolete] Export
Input
- Headers: Optional, it is the list of header columns in the CSV file. if it's null or empty, the CSV file has no header.
- FileName: Required.
- Data: Required, it's must be a
List<Dictionary<string, object>>
,List<object>
or a serialized JSON string of array object. - FileMode: Optional, value is
Create
orAppend
(Append is default).- If the FileMode is
Create
, the new file will be created and overridden the existing one on the temp folder. - If the FileMode is
Append
, the system opens the file if it exists and append the data from the end of the file, or creates a new file.
- If the FileMode is
- CultureInfo: Optional. It is a CSV format option, which will override the configuration (refer to section Configuration) if it is specified.
- Escape: Optional. It is a CSV format option, which will override the configuration (refer to section Configuration) if it is specified.
- Delimiter: Optional. It is a CSV format option, which will override the configuration (refer to section Configuration) if it is specified.
- DateTimeFormat: Optional. It is a CSV format option, which will override the configuration (refer to section Configuration) if it is specified.
- EncloseAll: Optional. It is a CSV format option, which will override the configuration (refer to section Configuration) if it is specified.
- EncloseCharacter: Optional. It is a CSV format option, which will override the configuration (refer to section Configuration) if it is specified.
- UTF8BOM: Optional. It is a CSV format option, which will override the configuration (refer to section Configuration) if it is specified.
Follow the below sample:
var parameters = new Dictionary<string, object>
{
{ "Headers" , new List<string>() },
{ "FileName", "fileName" },
{ "Data", JsonConvert.SerializeObject(data) },
{ "FileMode", "Create" },
{ "CultureInfo", "da-DK" },
{ "Escape", "\"" },
{ "Delimiter", "," },
{ "DateTimeFormat", "dd-MMM-yyyy HH:mm:ss" },
{ "EncloseAll", true },
{ "EncloseCharacter", "\"" },
{ "UTF8BOM", false }
};
Use the Invoke method in IPluginApi to call Export feature:
IPluginApi.Invoke("CwCsv", "Export", IDictionary<string, Object> parameters)
Output
The method returns data with two fields IsSuccess
, Error
.
When exported successfully, the CSV file will be created in the temp folder.
Example
var pluginApi = ctx.Use<IPluginApi>();
var dataApi = ctx.Use<IDataApi>();
var fileName = $"cw_csv_export_sample_{DateTime.Now.ToString("ddMMyyyyHHmmss")}.csv";
// Get data
var filter = FilterBuilder.Create()
.Eq("CaseState", "Created")
.Eq("IsDeleted", false)
.Build();
IList<DynamicDataObject> data = dataApi.Search(DataSourceApiQuery
.For("CwUsers")
.FilterBy(filter))
.Data;
// Construct plugin parameters
var parameters = new Dictionary<string, object>
{
{ "Headers" , new List<string>{ "Name", "Age" } },
{ "FileName", fileName },
{ "Data", JsonConvert.SerializeObject(data) },
{ "FileMode", "Create" }
};
// Invoke plugin
var pluginResult = pluginApi.Invoke("CwCsv", "Export", parameters);
var result = pluginResult.Data;
bool isSuccess = result["isSuccess"];
string errorMessage = result["Error"];
The following code is used to get the file path of the CSV file:
var filePath = Path.Combine(Path.GetTempPath(), fileName);
- Note: The function will not delete the file after exporting, we need to delete it manually after use.
Installation
Requirements
- Casewhere v2.6.0 or later.
Configuration
The plugin supports some settings while exporting to csv file, such as:
- CultureInfo: Name of the culture. Default is the culture of the thread that execute the plugin
- DateTimeFormat: Format for date-time value. Default is the full date-time format of the culture
- Delimiter: The delimiter to separate fields. Default is the list delimiter of the culture
- NewLine: the string that marks the new line. Default is the new line string of the running environment
- Encoding: Name of the encoding. Default is
UTF-8
- BigEndian: Value that indicates whether to use big endian for Unicode encoding or not. If the encoding does not support the big endianm it will be ignored. Default value is
false
- ByteOrderMark: Value that indicates whether to insert byte-order-mark in the exported file or not. If the encoding does not support, it will be ignored. Default value is
false
- EscapeCharacter: Special character that is needed to be escaped. Dafault value is
"
- EnclosingCharacter: Character that is used to enclose fields. Default value is
"
- EncloseAll: Value that indicates whether to wrap all fields in the enclosing character. Default value is
false
Example
{
"CultureInfo": "da-DK",
"DateTimeFormat": "dd-MM-yyyy HH:mm:ss",
"Delimiter": ",",
"NewLine": "\r\n",
"Encoding": "UTF-8",
"ByteOrderMark": false,
"EscapeCharacter": "\"",
"EnclosingCharacter": "\"",
"EncloseAll": true
}
Releases
1.0.0 - 01/03/2022
Changelog
- Export CSV file.
1.1.0 - 11/03/2022
Changelog
- Support header parameter when exporting file
1.2.0 - 23/05/2022
Changelog
- Reorganize CSV settings
- Add more CSV settings
- New method WriteData that handles headers more naturally
Download (login required) CSV plugin v1.2.0