Report processing actions are sent in an Ajax request to exchange data with the Web API service. You can handle post actions event to customize the Ajax requests.
AjaxBeforeLoad
AjaxSuccess
AjaxError
This event can be triggered before an Ajax request is sent to the Report Viewer Web API service. It allows you to set additional headers and custom data in the Ajax request. The following code sample demonstrates adding custom authorization header and passing default parameter values to service.
Initialize the ajaxBeforeLoad
event in the script and add the authorization token to the headers
property.
function App() {
return (<div id="viewer" style={viewerStyle}>
<BoldReportViewerComponent id="reportviewer-container"
reportServiceUrl = {'https://demos.boldreports.com/services/api/ReportViewer'}
reportPath = { 'Sales Order Detail.rdl' }
ajaxBeforeLoad = {onAjaxRequest}
>
</BoldReportViewerComponent>
</div>);
}
function onAjaxRequest(event) {
event.headers.push({
Key: 'Authorization', Value: 'demo@123'
});
}
In this tutorial, the
Sales Order Detail.rdl
report is used, and it can be downloaded from here.
Get the custom header value from the HttpContext
header collection using the key name specified at 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.
Use the data
property to set custom data to the server in the Ajax request. In the following code sample, parameter values are passed to the server side.
function App() {
return (<div id="viewer" style={viewerStyle}>
<BoldReportViewerComponent id="reportviewer-container"
reportServiceUrl = {'https://demos.boldreports.com/services/api/ReportViewer'}
reportPath = { 'Sales Order Detail.rdl' }
ajaxBeforeLoad = {onAjaxRequest}
>
</BoldReportViewerComponent>
</div>);
}
function 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 to change the datasource connection strings based on 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=<database>;"));
}
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=<database>;"));
}
}
}
To perform custom action or show user defined message, use the ajaxSuccess
event on the successful Ajax request.
function App() {
return (<div id="viewer" style={viewerStyle}>
<BoldReportViewerComponent id="reportviewer-container"
reportServiceUrl = {'https://demos.boldreports.com/services/api/ReportViewer'}
reportPath = { 'Sales Order Detail.rdl' }
ajaxSuccess = {onAjaxSuccess}
>
</BoldReportViewerComponent>
</div>);
}
function onAjaxSuccess(event) {
//Perform your custom success message here
alert("Ajax request success!!!");
}
The ajaxError
event is called, if an error occurred with the Ajax request. You can display the customized error details in the event method.
function App() {
return (<div id="viewer" style={viewerStyle}>
<BoldReportViewerComponent id="reportviewer-container"
reportServiceUrl = {'https://demos.boldreports.com/services/api/ReportViewer'}
reportPath = { 'Sales Order Detail.rdl' }
ajaxError = {onAjaxFailure}
>
</BoldReportViewerComponent>
</div>);
}
function onAjaxFailure(event) {
alert("Status: " + event.status + "\n" +
"Error: " + event.responseText);
}
You can never have both an error and a success callback with a request.