Articles in this section
Category / Section

How to Change the JSON Data at Runtime in Bold Reports

Published:
Updated:

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:

  1. 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);
        }
    
  2. 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.cs

    public 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
    	  };
    	}
    }
    
  3. 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:

    image.png

    JSON Data modified at runtime SNAP:

    image.png

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.

Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied