How to pass multiple values using the custom data
To pass multiple custom data values to the Web API service of the Bold Reports® Angular Report Viewer, send them as JSON through the ‘data’ property in the ajaxBeforeLoad event. Then deserialize them in the server-side API with a known class type and JsonConvert.DeserializeObject.
-
Map the
ajaxBeforeLoadevent with theonAjaxRequestfunction in the client to pass custom data to the server.<bold-reportviewer id="reportViewer_Control" [reportServiceUrl] = "serviceUrl" [processingMode] = "Remote" [reportServerUrl] = "serverUrl" [reportPath] = "reportPath" (ajaxBeforeLoad) = "onAjaxRequest($event)"> </bold-reportviewer>function onAjaxRequest(args) { //Passing custom data to server } -
Pass the multiple custom data values as JSON and use the
dataproperty to send the JSON string to the server in the Ajax request.function onAjaxRequest(args) { //Passing custom data to server var jsonData = { customerID: "CI0021", productID: "PO0022" }; args.data = jsonData; } -
The custom data values are stored in the
customDataheader key, and you can store them in the local property. The following code sample deserializes the JSON string and changes the data source connection strings based on the Customer ID and Product ID in theOnInitReportOptionsmethod.public SampleData customDatas = new SampleData(); public class SampleData { public string customerID { get; set; } public string productID { get; set; } } [HttpPost] public object PostReportAction([FromBody]Dictionary<string, object> jsonResult) { if (jsonResult.ContainsKey("customData")) { //Get client side JSON custom data, desirialize it and store in local variable. customDatas = JsonConvert.DeserializeObject<SampleData>(jsonResult["customData"].ToString()); } return ReportHelper.ProcessReport(jsonResult, this, this._cache); } public void OnInitReportOptions(ReportViewerOptions reportOption) { if (customDatas != null) { if (customDatas.customerID == "CI0021" && customDatas.productID == "PO0022") { //If you are changing the connection string based on customer id then could you please change the connection string as below. //reportOption.ReportModel.DataSourceCredentials.Add(new BoldReports.Web.DataSourceCredentials("<Datasource name>", "<connection string>")); reportOption.ReportModel.DataSourceCredentials.Add(new BoldReports.Web.DataSourceCredentials("<database>", "Data Source=<instancename>;Initial Catalog=<database>;")); } } }