Search results

Handle Post Actions

Report processing actions are sent in an Ajax request to exchange data with the Web API service. You can use the following Report Viewer events to customize the Ajax requests.

  • AjaxBeforeLoad
  • AjaxSuccess
  • AjaxError

AjaxBeforeLoad

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.

Add custom header to Ajax request

Initialize the ajaxBeforeLoad event in the script and add the authorization token to the headers property.

    <sf-report-viewer id="viewer" report-service-url="/api/ReportViewer" processing-mode="Remote" report-path="sales-order-detail.rdl" ajax-before-load="onAjaxRequest"></sf-report-viewer>

    <script type="text/javascript">
        function onAjaxRequest(args) {
            args.headers.push({ Key: 'Authorization', Value: btoa('guest', 'demo@123') });
        }
    </script>

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.

        private Microsoft.Extensions.Primitives.StringValues authenticationHeader;

        [HttpPost]
        public object PostReportAction([FromBody] Dictionary<string, object> jsonResult)
        {
            //jsonArray = jsonResult;

            if (jsonResult != null)
            {
                //Get client side custom ajax header and store in local variable
                HttpContext.Request.Headers.TryGetValue("Authorization", out authenticationHeader);

                //Perform your custom validation here
                if (authenticationHeader == "")
                {
                    return new Exception("Authentication failed!!!");
                }
                else
                {
                    return ReportHelper.ProcessReport(jsonResult, this, _cache);
                }
            }

            return ReportHelper.ProcessReport(jsonResult, this, this._cache);
        }

Perform your own action to validate the header values.

Pass custom data in Ajax request

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.

    <sf-report-viewer id="viewer" report-service-url="/api/ReportViewer" processing-mode="Remote" report-path="sales-order-detail.rdl" ajax-before-load="onAjaxRequest"></sf-report-viewer>

    <script type="text/javascript">
        function onAjaxRequest(args) {
            args.headers.push({ Key: 'Authorization', Value: btoa('guest', 'demo@123') });
            //Passing custom parameter data to server
            args.data = [{ name: 'SalesOrderNumber', labels: ['SO50756'], values: ['SO50756'] }];
        }
    </script>

The custom data values are stored in the CustomData header key, you can store it to the local property. The following code sample stores parameter values, then the values are set to the report in the OnReportLoaded method.

        public string DefaultParameter = null;
        private Microsoft.Extensions.Primitives.StringValues authenticationHeader;

        [HttpPost]
        public object PostReportAction([FromBody] Dictionary<string, object> jsonResult)
        {
            //jsonArray = jsonResult;

            if (jsonResult != null)
            {
                if (jsonResult.ContainsKey("CustomData"))
                {
                    //Get client side custom data and store in local variable. Here parameter values are sent.
                    DefaultParameter = jsonResult["CustomData"].ToString();
                }

                //Get client side custom ajax header and store in local variable
                HttpContext.Request.Headers.TryGetValue("Authorization", out authenticationHeader);

                //Perform your custom validation here
                if (authenticationHeader == "")
                {
                    return new Exception("Authentication failed!!!");
                }
                else
                {
                    return ReportHelper.ProcessReport(jsonResult, this, _cache);
                }
            }

            return ReportHelper.ProcessReport(jsonResult, this, this._cache);
        }

        public void OnReportLoaded(ReportViewerOptions reportOption)
        {
            if (DefaultParameter != null)
            {
                //Set client side custom header data
                reportOption.ReportModel.Parameters = JsonConvert.DeserializeObject<List<Syncfusion.Reporting.Web.ReportParameter>>(DefaultParameter);
            }
        }

AjaxSuccess

To perform custom action or show user defined message, use the ajaxSuccess event on the successful Ajax request.

    <sf-report-viewer id="viewer" report-service-url="/api/ReportViewer" processing-mode="Remote" report-path="sales-order-detail.rdl" ajax-before-load="onAjaxRequest" ajax-success="onAjaxSuccess"></sf-report-viewer>

    <script type="text/javascript">
        function onAjaxRequest(args) {
            args.headers.push({ Key: 'Authorization', Value: btoa('guest', 'demo@123') });

            //Passing custom parameter data to server
            args.data = [{ name: 'SalesOrderNumber', labels: ['SO50756'], values: ['SO50756'] }];
        }

        function onAjaxSuccess(args) {
            //Perform your custom success message here
            alert("Ajax request success!!!");
        }
    </script>

AjaxError

The ajaxError event is called, if an error occurred with the request, you can display the customized error detail in the event method.

    <sf-report-viewer id="viewer" report-service-url="/api/ReportViewer" processing-mode="Remote" report-path="sales-order-detail.rdl" ajax-before-load="onAjaxRequest" ajax-error="onAjaxFailure"></sf-report-viewer>

    <script type="text/javascript">
        function onAjaxRequest(args) {
            args.headers.push({ Key: 'Authorization', Value: btoa('guest', 'demo@123') });

            //Passing custom parameter data to server
            args.data = [{ name: 'SalesOrderNumber', labels: ['SO50756'], values: ['SO50756'] }];
        }
        function onAjaxFailure(args) {
            alert("Status: " + args.status + "\n" +
                "Error: " + args.responseText);
        }
    </script>

You can never have both an error and a success callback with a request.