cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

What is an InfoTable?

100% helpful (2/2)

Everywhere in the Thingworx Platform (even the edge and extensions) you see the data structure called InfoTables.  What are they?  They are used to return data from services, map values in mashup and move information around the platform.  What they are is very simple, how they are setup and used is also simple but there are a lot of ways to manipulate them.  Simply put InfoTables are JSON data, that is all.  However they use a standard structure that the platform can recognize and use.

There are two peices to an InfoTable, the DataShape definition and the rows array.  The DataShape is the definition of each row value in the rows array.  This is not accessible directly in service code but there are function and structures to manipulate it in services if needed.

Example InfoTable Definitions and Values:

{ dataShape: {

    fieldDefinitions : {

          name: "ColOneName", baseType: "STRING"

    },

    {

          name: "ColTwoName", baseType: "NUMBER"

    },

rows: [

    {ColOneName: "FirstValue", ColTwoName: 13},

    {ColOneName: "SecondValue, ColTwoName: 14}

    ]

}

So you can see that the dataShape value is made up of a group of JSON objects that are under the fieldDefinitions element.  Each field is a "name" element, which of course defined the field name, and the "baseType" element which is the Thingworx primitive type of the named field.  Typically this structure is automatically created by using a DataShape object that is defined in the platform.  This is also the reason DataShapes need to be defined, so that fields can be defined not only for InfoTables, but also for DataTables and Streams.  This is how Mashups know what the structure of the data is when creating bindings to widgets and other parts of the platform can display data in a structured format.

The other part is the "rows" element which contains an array of of JSON objects which contain the actual data in the InfoTable.

Accessing the values in the rows is as simple as using standard JavaScript syntax for JSON.  To access the number in the first row of the InfoTable referenced above (if the name of the InfoTable variable is "MyInfoTable") is done using MyInfoTable.rows[0].ColTowName.  This would return a value of 13.  As you can not the JSON array index starts at zero.

Looping through an InfoTable in service script is also very simple.  You can use the index in a standard "for loop" structure, but a little cleaner way is to use a "for each loop" like this...

for each (row in MyInfoTable.rows) {

    var colOneVal = row.ColOneName;

    ...

}

It is important to note that outputs of many base services in the platform have an output of the InfoTable type and that most of these have system defined datashapes built into the platform (such as QueryDataTableEntries, GetImplimentingThings, QueryNumberPropertyHistory and many, many more).  Also all service results from query services accessing external databases are returned in the structure of an InfoTable.

Manipulating an InfoTable in script is easy using various functions built into the platform.  Many of these can be found in the "Snippets" tab of the service editor in Composer in both the InfoTableFunctions Resource and InfoTable Code Snippets. Some of my favorites and most commonly used...

Create a blank InfoTable:

var params = {

  infoTableName: "MyTable"

};

var MyInfoTable= Resources["InfoTableFunctions"].CreateInfoTable(params);

Add a new field to any InfoTable:

MyInfoTable.AddField({name: "ColNameThree", baseType: "BOOLEAN"});

Delete a field:

MyInfoTable.RemoveField("ColNameThree");

Add a data row:

MyInfoTable.AddRow({ColOneName: "NewRowValue", ColTwoName: 15});

Delete one or more data row matching the values defined (Note you can define multiple field in this statement):

//delete all rows that have a value of 13 in ColNameOne

MyInfoTable.Delete({ColNameOne: 13});

Create an InfoTable using a predefined DataShape:

var params = {

  infoTableName: "MyInfoTable",

  dataShapeName: "dataShapeName"

};

var MyInfoTable = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

There are many more functions built into the platform, including ones to filter, sort and query rows.  These can be extremely useful when tying to return limited or more strictly structured InfoTable data.  Hopefully this gives you a better understanding and use of this critical part of the Thingworx Platform.

Comments

I wanted to throw in a link to a short paper I wrote on InfoTables that might also be helpful. Getting to Know InfoTables.pdf

I want to also point out because, it's not always obvious what to do when starting out, that an InfoTable property should not be solely relied on to persist data when services and subscriptions are acting upon the table to update and add rows. There should be some other storage mechanism, such as a DataTable, where all rows are stored if you desire to keep them around between property updates. Also, if rows are going to be added to an InfoTable property via a custom service, the property should first be cloned to another variable and then the cloned representation is what will be manipulated and then set to the property upon completion.

Version history
Last update:
‎Oct 24, 2015 12:34 PM
Updated by:
Labels (1)
Tags (1)