How to Change the JSON Data at Runtime in Bold Reports
Bold Reports is a powerful reporting tool that allows you to create and render reports from a variety of data sources. In addition to traditional data sources, Bold Reports also supports JSON data. In Bold Reports, we can change the JSON data through an API dynamically at runtime and render the reports. We can achieve this requirement by deserializing the JSON data (read as a text file) and loading it into the report at runtime in the OnReportLoaded API.
To pass JSON data for a report in Bold Reports through an API at runtime, you can follow the steps outlined below:
-
The data source information is stored in JSONResult, and you can store it in the local property.
private Dictionary<string, object> _jsonResult; public object PostReportAction([FromBody] Dictionary<string, object> jsonArray) { _jsonResult = jsonArray; return ReportHelper.ProcessReport(jsonArray, this, this._cache); }
-
Utilize the jsonResult information with the ReportHelper.GetDatasource API to retrieve the data source details of the reports. Then need to change the connection string of the data source by using the DataSourceCredentials object.
ReportViewerController.cspublic void OnReportLoaded(ReportViewerOptions reportOption) { string basePath = _hostingEnvironment.WebRootPath; List<DataSourceInfo> datasources = ReportHelper.GetDataSources(_jsonResult, this, _cache); string jsonValue = System.IO.File.ReadAllText(basePath + @"\Resources\New_text.json"); foreach (DataSourceInfo item in datasources) { FileDataModel model = new FileDataModel(); model.DataMode = "inline"; model.Data = jsonValue; item.DataProvider = "JSON"; DataSourceCredentials DataSourceCredentials = new DataSourceCredentials(); DataSourceCredentials.Name = item.DataSourceName; DataSourceCredentials.UserId = null; DataSourceCredentials.Password = null; DataSourceCredentials.ConnectionString = JsonConvert.SerializeObject(model); DataSourceCredentials.IntegratedSecurity = false; reportOption.ReportModel.DataSourceCredentials = new List<DataSourceCredentials> { DataSourceCredentials }; } }
-
Build and run your application. The report will now render with the JSON data attached, which was loaded from the “New_text.json” file.
Designer Preview SNAP:
JSON Data modified at runtime SNAP:
By following these steps, you can pass JSON data for a report in Bold Reports via an API at runtime, dynamically loading the data into the report for rendering.