Page 1 of 1

Programmatically add OData datasource

Posted: Tue Oct 03, 2017 12:49 pm
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);
            };
        }

Re: Programmatically add OData datasource

Posted: Tue Oct 03, 2017 5:59 pm
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.

Re: Programmatically add OData datasource

Posted: Wed Oct 04, 2017 8:26 am
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"

Re: Programmatically add OData datasource

Posted: Thu Oct 05, 2017 12:42 pm
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.

Re: Programmatically add OData datasource

Posted: Thu Oct 05, 2017 1:59 pm
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?

Re: Programmatically add OData datasource

Posted: Thu Oct 05, 2017 3:05 pm
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.

Re: Programmatically add OData datasource

Posted: Tue Oct 10, 2017 12:03 pm
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...

Re: Programmatically add OData datasource

Posted: Wed Oct 11, 2017 1:25 pm
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.