In this post, we’ll walk through using the Web Service API of InVision to read from the database. As I mentioned in Part 1, InVision is built using AngularJS, so some experience with that framework is required.

You should download the source code from the link above to follow along as I explain the parts of the code that’s relevant to understanding using the Web Service API.

Reading data using the Sql Script Web Service API

The source code has a function called loadIncidentTypes. This function loads incident types from the database through the Web Service API for executing SQL Scripts, so that a list of options can be presented to the user.

There are basically 3 parts of this code that we should look closer at:

The applicationContext service.
When your plugin component inherits from the PluginComponent base class, the applicationContext service is automatically injected into your component. Through the applicationContext service, you get access to the servics and interaction model of the Workbook.
The service I’m using here, is the ‘httpClients’ service. Through the httpClient service, you get access to the different Web Service APIs that you can use to access resources in InVision.

Through the httpClients service, I’m getting a reference to the SqlScriptHttpClient. The SqlScriptHttpClient is used for calling into the Sql Script Web Service, and has a single method, execute. The execute method takes two arguments; the id of the Sql Script and a message body containing the language id of the user and a params object (they are both optional unless your script is parameterized). If you look at the sample code, GET_INCIDENTTYPES_SCRIPTID is just a string with the id of the Sql Script we want to execute (get it by right clicking the node in the Solution Explorer).

The execute method returns a promise, and when it’s done we’re converting the result into an array of objects (rows) that we’ll present to the user. Note that the Sql Script is set up to return data, otherwise we would get nothing back even if the call succeeds.
Data returned from most InVision Web Service APIs are wrapped as a package with a header and the acutal data serialized, so that the overall size of the payload is smaller than it would be if the data was returned as, say, standard JSON. The header describes how the compressed data can be deserialized into an array of objects, and the DataPackageReader is the service we can use for this task. So for example, if your Sql Script returns information about people, the result of sending that through DataPackageReader.createObjectArray may look something like this: