MicroStrategy ONE

Connecting to a CSV file

The sample Javascript code provided below creates a simple connector object that uses CSV data from a single table on the data source. It assumes that you have already created an HTML page that points to the this Javascript file. The sample code is followed by an explanation of important parts of the code.

Your Javascript code must do the following:

Create a connector object

Copy
(function(){
  // mstr is a global object from mstrgdc-2.0.js, which represents the data connector framework
  var myConnector = mstr.createDataConnector();
  // Connector must define fetchTable function
  myConnector.fetchTable = function(table, params, doneCallback) {
    // params represents information sent by connector to MSTR at interactive phase
    var mstrObj = JSON.parse(params);
    var file = mstrObj.connectionData.file;
    var url = file;
    $.get(url, function(resp) {
      table.appendRawData(resp)
      doneCallback(table);
    });
  };
  // validateDataConnector does a validation check of the connector
  mstr.validateDataConnector(myConnector);
})();
  • mstr is the global object. This object is initialized after the SDK library is loaded.
  • The createDataConnector function is used to create an object of GenericDataConnector.
  • The connector must implement the fetchTable function, which is used to get data from the data source. In this case, it loads the CSV file located in the same folder as the HTML file.
  • In this connector, fetchTable uses appendRawData to append data. This function does not require schema information. For data with schema, see Connecting to data with schema that you define.
  • The validateDataConnector function is used to validate the connector object.

Add an event listener

Copy
// Create event listener for when the user submits the form
$(document).ready(function() {
  $("#submitButton").click(function() {
    var content = $("#file").val();
    mstr.connectionName = "RawDataFiles";
    // connectionData is a JSON object. Connector can put any information here.
    mstr.connectionData = {};
    mstr.connectionData.file = content;
    // MUST define tableList field. Can import multiple tables in one connection.
    mstr.tableList = [];
    mstr.tableList.push({tableName: "RawDataCsv"});
    // Inform that interactive phase is finished and send information to MSTR
    window.mstr.submit();
  });
});
  • connectionData is a JSON object where the connector can put customizable data. This information will be passed to the fetchTable function when it is invoked at the Intelligence Server. In this case, the fetchTable function expects the file name of a CSV file located in the same folder as the HTML file, so you should have put the CSV file in that folder and enter its name on the HTML page.
  • tableList MUST be defined to inform MicroStrategy how many tables should be fetched in the current connection. For this sample, the array includes only one table, called "RawDataCsv".

Test and run the connector

To test and run this connector, you put a CSV file in the same folder as the HTML file. Once the data connector has been registered, you enter the file name (with the file extension) on the HTML page to import that file.