Handle post actions
To customize the Ajax requests that the Bold Reports® Angular Report Viewer sends to the Web API service, handle the following post action events. Report processing actions are sent in an Ajax request to exchange data with the Web API service.
AjaxBeforeLoadAjaxSuccessAjaxError
AjaxBeforeLoad
The ajaxBeforeLoad event of the Angular Report Viewer can be triggered before an Ajax request is sent to the Report Viewer Web API service. The event lets you set additional headers and custom data in the Ajax request. The following code samples demonstrate adding a custom authorization header and passing default parameter values to the service.
How do I add a custom header to the Ajax request?
To add a custom header to the Angular Report Viewer Ajax request, initialize the ajaxBeforeLoad event in the script and add the authorization token to the headers property.
<bold-reportviewer id="reportViewer_Control"
[reportServiceUrl] = "serviceUrl"
[processingMode] = "Remote"
[reportServerUrl] = "serverUrl"
[reportPath] = "reportPath"
(ajaxBeforeLoad) = "onAjaxRequest($event)">
</bold-reportviewer>import { Component, ViewChild } from '@angular/core';
import { BoldReportsAngularModule } from '@boldreports/angular-reporting-components/src/core';
@Component({
selector: 'ej-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
public serviceUrl: string;
public reportPath: string;
public serverUrl: string;
constructor() {
this.serviceUrl = 'https://demos.boldreports.com/services/api/ReportApi';
this.reportPath = 'Sales Order Detail.rdl';
}
onAjaxRequest(event) {
event.headers.push({ Key: 'Authorization', Value: 'demo@123' });
}
}This tutorial uses the
Sales Order Detail.rdlreport, which you can download from the sales order detail sample report download.
Get the custom header value from the HttpContext header collection using the key name specified on the client side.
string authenticationHeader;
public object PostReportAction(Dictionary<string, object> jsonResult)
{
if (jsonResult != null)
{
//Get client side custom ajax header and store in local variable
authenticationHeader = HttpContext.Current.Request.Headers["Authorization"];
//Perform your custom validation here
if (authenticationHeader == "")
{
return new Exception("Authentication failed!!!");
}
else
{
return ReportHelper.ProcessReport(jsonResult, this);
}
}
return null;
}Perform your own action to validate the header values.
How do I pass custom data in the Ajax request?
To pass custom data to the server in the Angular Report Viewer Ajax request, use the data property. In the following code sample, parameter values are passed to the server side.
<bold-reportviewer id="reportViewer_Control"
[reportServiceUrl] = "serviceUrl"
[processingMode] = "Remote"
[reportServerUrl] = "serverUrl"
[reportPath] = "reportPath"
(ajaxBeforeLoad) = "onAjaxRequest($event)">
</bold-reportviewer>import { Component, ViewChild } from '@angular/core';
import { BoldReportsAngularModule } from '@boldreports/angular-reporting-components/src/core';
@Component({
selector: 'ej-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
public serviceUrl: string;
public reportPath: string;
public serverUrl: string;
constructor() {
this.serviceUrl = 'https://demos.boldreports.com/services/api/ReportApi';
this.reportPath = 'Sales Order Detail.rdl';
}
onAjaxRequest(event) {
//Passing custom data to server
var customerID = "CI0021";
event.data = customerID;
}
}The custom data values are stored in the customData header key, and you can store them in the local property. The following code sample demonstrates how to change the data source connection strings based on the Customer ID in the OnInitReportOptions method.
string customerID = null;
public object PostReportAction(Dictionary<string, object> jsonResult)
{
if (jsonResult != null)
{
if (jsonResult.ContainsKey("customData"))
{
//Get client side custom data and store in local variable.
customerID = jsonResult["customData"].ToString();
}
}
return ReportHelper.ProcessReport(jsonResult, this);
}
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
if (customerID != null)
{
if(customerID.Contains("CI0021"))
{
//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=NorthwindSdf;"));
}
else if(customerID.Contains("CI0022"))
{
//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=NorthwindSdf;"));
}
}
}AjaxSuccess
To perform a custom action or show a user-defined message after a successful Ajax request, use the ajaxSuccess event of the Angular Report Viewer.
<bold-reportviewer id="reportViewer_Control"
[reportServiceUrl] = "serviceUrl"
[processingMode] = "Remote"
[reportServerUrl] = "serverUrl"
[reportPath] = "reportPath"
(ajaxSuccess) = "onAjaxSuccess($event)">
</bold-reportviewer>import { Component, ViewChild } from '@angular/core';
import { BoldReportsAngularModule } from '@boldreports/angular-reporting-components/src/core';
@Component({
selector: 'ej-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
public serviceUrl: string;
public reportPath: string;
public serverUrl: string;
constructor() {
this.serviceUrl = 'https://demos.boldreports.com/services/api/ReportApi';
this.reportPath = 'Sales Order Detail.rdl';
}
onAjaxSuccess(event) {
//Perform your custom success message here
alert("Ajax request success!!!");
}
}AjaxError
The ajaxError event of the Angular Report Viewer is called if an error occurs with the Ajax request. You can display customized error details in the event method.
<bold-reportviewer id="reportViewer_Control"
[reportServiceUrl] = "serviceUrl"
[processingMode] = "Remote"
[reportServerUrl] = "serverUrl"
[reportPath] = "reportPath"
(ajaxError) = "onAjaxFailure($event)">
</bold-reportviewer>import { Component, ViewChild } from '@angular/core';
import { BoldReportsAngularModule } from '@boldreports/angular-reporting-components/src/core';
@Component({
selector: 'ej-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
public serviceUrl: string;
public reportPath: string;
public serverUrl: string;
constructor() {
this.serviceUrl = 'https://demos.boldreports.com/services/api/ReportApi';
this.reportPath = 'Sales Order Detail.rdl';
}
onAjaxFailure(event) {
alert("Status: " + event.status + "\n" +
"Error: " + event.responseText);
}
}You can never have both an error and a success callback with a request.