Data Export Scripts

<< <%SKIN-STRTRANS-SYNTOC%> >>

Navigation:  Scripting >

Data Export Scripts

Data Export scripts can be used to customize the export process. An export script could be used to export extracted data to custom data structures in a database, or it could export data to a custom data source.

 

A Data Export script can be added from the Data Export Configuration screen:

 

dataConfiguration

 

The following example writes data from two export tables to two CSV files:

using System;

using System.IO;

using Sequentum.ContentGrabber.Api;

using Sequentum.ContentGrabber.Commands;

public class Script

{        

 public static bool ExportData(DataExportArguments args)

 {

         try

         {

                 StreamWriter writer1 = File.CreateText(@"c:\temp\data1.csv");

                 try                        

                 {

                         StreamWriter writer2 = File.CreateText(@"c:\temp\data2.csv");

                         try

                         {

                                 IExportReader mainTableReader = args.Data.GetTable();

                                 try

                                 {

                                         while(mainTableReader.Read())

                                         {

                                                 string csvData1 = childTableReader.

GetStringValue("Field Name 1");

                                                 csvData1 += ",";

                                                 csvData1 += childTableReader.GetStringValue
("Field Name 2");

                                                 writer1.WriteLine(csvData1);

                                                 

                                                 IExportReader childTableReader = 

mainTableReader.GetChildTable("Child table name");                        

                                                 try

                                                 {

                                                         while(childTableReader.Read())

                                                         {

                                                                 string csvData2 = 

childTableReader.GetStringValue("Child Field Name 1");

                                                                 csvData2 += ",";

                                                                 csvData2 += childTableReader.
GetStringValue("Child Field Name 2");

                                                                 writer2.WriteLine(csvData2);                                                

                                                         }

                                                 }

                                                 finally

                                                 {                                

                                                         childTableReader.Close();

                                                 }

                                         }

                                 }

                                 finally

                                 {

                                         mainTableReader.Close();

                                 }

                         }

                         finally

                         {

                                 writer1.Close();

                         }

                 }

                 finally

                 {

                         writer2.Close();

                 }

                 return true;

         }

         catch(Exception ex)

         {

                 args.WriteDebug(ex.Message, DebugMessageType.Error);

                 return false;

         }

 }

}

 

The script must have a static method with the following signature:

 

public static bool ExportData(DataExportArguments args)

 

The function should return True if it succeeds and False if it fails. In this example, an instance of the DataExportArguments class will be given by Content Grabber and has the following functions and properties:

 

Property or Function

Description

Agent Agent

The current agent.

ScriptUtils ScriptUtilities

A script utility class with helper methods. See Script Utilities for more information.

IExportData Data

The data to export.

bool IsDebug

True if the agent is running in debug mode.

ExtendedSortedList<string, string> InputParemeters

All input parameters defined by the agent.

void WriteDebug(string debugMessage, DebugMessageType messageType = DebugMessageType.Information)

Writes log information to the agent log. This method has no effect if agent logging is disabled, or if called during design time.

void WriteDebug(string debugMessage, bool showMessageInDesignMode, DebugMessageType messageType = DebugMessageType.Information)

Writes log information to the agent log. This method has no effect if agent logging is disabled, or if called during design time.

void Notify(bool alwaysNotify)

Triggers notification at the end of an agent run. If alwaysNotify is set to false, this method only triggers a notification if the agent has been configured to send notifications on critical errors.

void Notify(string message, bool alwaysNotify)

Triggers notification at the end of an agent run, and adds the message to the notification email. If alwaysNotify is set to false, this method only triggers a notification if the agent has been configured to send notifications on critical errors.

GlobalDataDictionary GlobalData

Global data dictionary that can be used to store data that needs to be available in all scripts and after agent restarts.

 

Input Parameters are also stored in this dictionary.

IConnection GetDatabaseConnection(string connectionName)

Returns the specified database connection. The database connection must have been previously defined for the agent or be a shared connection for all agents on the computer. Your script is responsible for opening and closing the connection by calling the OpenDatabase and CloseDatabase methods.

string[] ExportData(ExportTarget exportTarget, string sessionId = null)

Exports data to the specified export target. Returns the names of any exported data files if exporting to a file format.

void ExportToDatabase(string databaseConnectionName, string sessionId = null)

Exports data to the specified database.

string[] ExportToCsv(string sessionId = null)

Exports data to a CSV file using the default export folders and default options. Use the ExportData method to specify CSV export options. Returns the names of all exported CSV files.

string ExportToExcel(string sessionId = null)

Exports data to an Excel file using the default export folders and default options. Use the ExportData method to specify Excel export options. Returns the name of the exported Excel file.

string ExportToXml(string sessionId = null)

Exports data to an XML file using the default export folders and default options. Use the ExportData method to specify XML export options. Returns the name of the exported XML file.

void DistributeData(string file)

Distributes data to the target configured in the agent.

void DistributeData(string[] files)

Distributes data to the target configured in the agent.

 

The IExportData interface has the following functions and properties:

Property or Function

Description

IExportReader GetTable()

Returns a data reader that reads data from the main table containing exported data. All other data tables will be child tables of this table.

IExportReader this[string name] { get; }

Returns a data reader that reads data from a specified data table.

IExportReader GetTable(string name)

Returns a data reader that reads data from a specified data table.

bool ValidateData()

Validates the export data and returns True if the data is valid and False if the data is invalid. If the agent that extracted this data has changed since the data was extracted, then the data may have become invalid.

 

The IExportReader interface has the following functions and properties:

Property or Function

Description

bool Read()

Reads the next data row. Returns False if there are no more data rows available.

void Close()

Closes the data reader.

string GetStringValue(string columnName)

Gets a String value from the current row.

int GetIntValue(string columnName)

Gets a Integer value from the current row.

Guid GetGuidValue(string columnName)

Gets a GUID value from the current row.

string GetStringValue(int index)

Gets a String value from the current row.

int GetIntValue(int index)

Gets a Integer value from the current row.

Guid GetGuidValue(int index)

Gets a GUID value from the current row.

Type GetFieldType(int index)

Gets the data type of a specified column.

object GetFieldValue(string columnName)

Return a data value from the current data row.

object GetFieldValue(int index)

Return a data value from the current data row.

object this[string columnName]

Return a data value from the current data row.

object this[int index]

Return a data value from the current data row.

IExportReader GetChildTable(string tableName)

Return a data reader that reads data from a specified child table of the table associated with this data reader. The returned data reader will only read data rows that are child data to the current data row.

string[] ChildTableNames

An array containing the names of all child tables of the table associated with this data reader.