Programmatically add OData datasource

Stimulsoft Reports.JS discussion
Post Reply
s.wieczorek
Posts: 19
Joined: Fri Sep 22, 2017 5:06 am

Programmatically add OData datasource

Post by s.wieczorek »

Hi,
How can we programmatically add OData datasource?
The following code doesn't work:

Code: Select all

function init() {
            var options = new window.Stimulsoft.Designer.StiDesignerOptions();
            options.appearance.fullScreenMode = true;
            var designer = new window.Stimulsoft.Designer.StiDesigner(options, 'StiDesigner', false);
            var report = new window.Stimulsoft.Report.StiReport();


            report.dictionary.databases.clear();
            var connStr = "http://localhost:8882/stimulsoft_proxy_action/";
            var database = new Stimulsoft.Report.Dictionary.StiODataDatabase("EPBP", "EPBP", connStr);


            report.dictionary.databases.add(database);
            report.dictionary.synchronize();
            report.render();

            console.log(report);
            designer.report = report;
            designer.renderHtml('designerContent');

            designer.onSaveReport = function (event) {
                console.log('dataBases', report.dictionary.databases);
                console.log(event);
            };
        }
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Programmatically add OData datasource

Post by Alex K. »

Hello,

The report.dictionary.synchronize() works only for XML data. In other cases, you should add tables in your code:
for example:

Code: Select all

    var dataSource = new Stimulsoft.Report.Dictionary.StiDataTableSource(data.tableName, data.tableName, data.tableName);
    dataSource.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("Column1", "Column1", "Column1"));
    dataSource.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("Column2", "Column2", "Column2"));
    report.dictionary.dataSources.add(dataSource);
Thank you.
s.wieczorek
Posts: 19
Joined: Fri Sep 22, 2017 5:06 am

Re: Programmatically add OData datasource

Post by s.wieczorek »

Could you specify more information? Or at least indicate where to find a similar example?
How to connect to OData service from within StiDataTableSource?

i'm getting an error with the following code snippet:

Code: Select all

      function init() {
            var options = new window.Stimulsoft.Designer.StiDesignerOptions();
            options.appearance.fullScreenMode = true;
            var designer = new window.Stimulsoft.Designer.StiDesigner(options, 'StiDesigner', false);
            var report = new window.Stimulsoft.Report.StiReport();


            report.dictionary.databases.clear();
            var connStr = "http://localhost:8882/stimulsoft_proxy_action/";
            var database = new Stimulsoft.Report.Dictionary.StiODataDatabase("MyDataSource", "OData", connStr);

            report.dictionary.databases.add(database);

            var dataSource = new Stimulsoft.Report.Dictionary.StiDataTableSource("reservationsInfoView", "reservationsInfoView", "reservationsInfoView");
            dataSource.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("_id", "_id", "_id"));
            dataSource.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("reservationNumber", "reservationNumber", "reservationNumber"));
            report.dictionary.dataSources.add(dataSource);

            report.dictionary.synchronize();

            report.render();

            console.log(report);
            designer.report = report;
            designer.renderHtml('designerContent');

            designer.onSaveReport = function (event) {
                console.log('dataBases', report.dictionary.databases);
                console.log(event);
            };
        }
invoking report.dictionary.synchronize(); throws an error (browsers console):

Code: Select all

TypeError: s.viewData is null stimulsoft.reports.js:143:224561
	a</s.prototype.getDataFromDataSource http://localhost:8882/scripts/stimulsoft.reports.js:143:224561
	a</s.prototype.connectDataSourceToData http://localhost:8882/scripts/stimulsoft.reports.js:143:225241
	B</T.prototype.connect http://localhost:8882/scripts/stimulsoft.reports.js:143:210901
	p</r.prototype.connect http://localhost:8882/scripts/stimulsoft.reports.js:143:288183
	d</d.prototype.connect http://localhost:8882/scripts/stimulsoft.reports.js:144:210672
	d</d.prototype.synchronize http://localhost:8882/scripts/stimulsoft.reports.js:144:203988
	init http://localhost:8882/report/test:58:13
	onload
Also when i remove line report.dictionary.synchronize(); from code i can see added datasources. But when dragged fields into table and try to preview them I'm getting the same error as above: "s.viewData is null"
Attachments
datasource2.jpg
datasource2.jpg (145.2 KiB) Viewed 4523 times
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Programmatically add OData datasource

Post by Alex K. »

Hello,

In this case, you should use the following code:

Code: Select all

report.dictionary.databases.clear();
var connStr = "http://localhost:8882/stimulsoft_proxy_action/";
var database = new Stimulsoft.Report.Dictionary.StiODataDatabase("MyDataSource", "OData", connStr);

report.dictionary.databases.add(database);

var dataSource = new Stimulsoft.Report.Dictionary.StiODataSource("MyDataSource", "reservationsInfoView", "reservationsInfoView");
dataSource.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("_id", "_id", "_id"));
dataSource.columns.add(new Stimulsoft.Report.Dictionary.StiDataColumn("reservationNumber", "reservationNumber", "reservationNumber"));
report.dictionary.dataSources.add(dataSource);
Thank you.
s.wieczorek
Posts: 19
Joined: Fri Sep 22, 2017 5:06 am

Re: Programmatically add OData datasource

Post by s.wieczorek »

Thank you. It's working well. Another question is that we need to add some relations between tables (due to your component doesn't support complex types we will be working on mongodb views with flat structure). How can we add relations programmatically?
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Programmatically add OData datasource

Post by Alex K. »

Hello,

You can use the following code:

Code: Select all

var dataRelation = new Stimulsoft.Report.Dictionary.StiDataRelation("MyRelation", "MyRelation", "MyRelation", report.dictionary.dataSources.getByName("Categories"), report.dictionary.dataSources.getByName("Products"), ["CategoryID"], ["CategoryID"]);
report.dictionary.relations.add(dataRelation); 
Thank you.
s.wieczorek
Posts: 19
Joined: Fri Sep 22, 2017 5:06 am

Re: Programmatically add OData datasource

Post by s.wieczorek »

Thank you. Another question is how to set a datatype to certain column? For example to have a StiDataColumn being a boolean type...
HighAley
Posts: 8431
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Programmatically add OData datasource

Post by HighAley »

Hello.

There is another constructor that you could use:

Code: Select all

public StiDataColumn(string nameInSource, string name, string alias, System.Type type)
Thank you.
Post Reply