HTML5 Report Viewer with dynamic Report file

4 posts, 0 answers
  1. Marcin
    Marcin avatar
    165 posts
    Member since:
    Jan 2011

    Posted 31 Oct 2013 Link to this post

    Hello,
    Currently I'm using Telerik Reporting as internal rendering process of file request. During initialization in MVC application I have following code:
    var reportDefinitionFile = Path.Combine(dataFolder, company, "Notice.trdx");
     
                if (!System.IO.File.Exists(reportDefinitionFile))
                {
                    throw new HttpException((int)HttpStatusCode.NotFound, "File not found.");
                }
     
                var xmlSerializer = new ReportXmlSerializer();
                var reportDocument = (Report)xmlSerializer.Deserialize(reportDefinitionFile);
     
                reportDocument.ReportParameters["ContactId"].Value = n.ToString("D");
     
                var sds = (SqlDataSource)reportDocument.DataSource;
                sds.ConnectionString = database.ConnectionString;
     
                this.SetConnectionString(reportDocument.Items, database.ConnectionString);
     
                // create a ReportSource object
                var reportSource = new InstanceReportSource { ReportDocument = reportDocument };
                var reportProcessor = new ReportProcessor();
     
                RenderingResult result = reportProcessor.RenderReport("PDF", reportSource, null);
    So as you can see I render file and return it normaly as MVC File
    return File(result.DocumentBytes, "application/pdf", "File.pdf");
    I'm also changing internal connection string because the same report can run against different databases:
    private void SetConnectionString(IEnumerable<ReportItemBase> items, string connectionString)
            {
                foreach (ReportItemBase ib in items)
                {
                    if (ib.Items != null)
                    {
                        this.SetConnectionString(ib.Items, connectionString);
                    }
     
                    Type type = ib.GetType();
                    PropertyInfo dataSourceProperty = type.GetProperty("DataSource");
                    if (dataSourceProperty == null)
                    {
                        continue;
                    }
     
                    var dataSource = dataSourceProperty.GetValue(ib, null) as System.Web.UI.WebControls.SqlDataSource;
                    if (dataSource != null)
                    {
                        dataSource.ConnectionString = connectionString;
                    }
                }
            }

    My question now is how to use report generated by line:
    var reportSource = new InstanceReportSource { ReportDocument = reportDocument };
     in HTML5 MVC application?

    I cannot find any example how to use such automatic report in new functionality.
    Could you help me?
  2. IvanY
    Admin
    IvanY avatar
    417 posts

    Posted 01 Nov 2013 Link to this post

    Hi Marcin,

    The supported report sources are listed in the jQuery.fn.telerik_Report'Viewer(options) help article. 

    You can use TypeReportSource and pass reports to the HTML5 Report Viewer via assembly qualified name or use UriReportSource and pass the report path. Both support ObjectDataSource which may receive parameters and may contain all the necessary custom logic you have to implement on order to return the correct data.

    InstanceReportSource (i.e., live modified report object) is not supported because of the specifics of the REST service scenario where there is no live session state that keeps live objects between the resource requests.

    For more information how to pass parameters to your reports look at the HTML5 Report Viewer article How To: Pass values to report parameters help article.

    Let us know if you have any further questions.

    Regards,
    IvanY
    Telerik

    Have you tried the new visualization options in Telerik Reporting Q2 2013? You can get them from your account.

  3. DevCraft banner
  4. Marcin
    Marcin avatar
    165 posts
    Member since:
    Jan 2011

    Posted 01 Nov 2013 Link to this post

    Hello,
    from what I'm seeing i can do what i need with resolver. Is there any example somwhere how to implement custom resolver?
    Regards
    Marcin
  5. Stef
    Admin
    Stef avatar
    3047 posts

    Posted 06 Nov 2013 Link to this post

    Hi Marcin,

    For your requirements you can use the new REST service and just get the PDF export of a report definition. For more details take a look at the REST API: Get Document help article. For example, this is a sample script illustrating the flow of the requests:
    var clientid = "";
     var instanceid = "";
     $.ajax({
         type: "Post",
         url: '/api/reports/clients',
         contentType: "application/json",
         data: {},
         success: function (data) {
     
             clientid = data.toString();
             var myreport = new Object();
             myreport.report = "Telerik.Reporting.Examples.CSharp.ReportCatalog, CSharp.ReportLibrary";
             myreport.parameterValues = [""];
     
             var jsonText = JSON.stringify(myreport,  "\t");
     
             $.ajax({
                 type: "Post",
                 url: '/api/reports/clients/' + clientid + '/instances',
                 contentType: "application/json",
                 data: jsonText,
                 success: function (data) {
                     instanceid = data.toString();
     
                     var output = new Object();
                     output.format = "PDF";
                     var jsonText1 = JSON.stringify(output, "\t");
                     $.ajax({
                         type: "Post",
                         url: '/api/reports/clients/' + clientid + '/instances/' + instanceid + '/documents',
                         contentType: "application/json",
                         data: jsonText1,
                         success: function (data) {
     
                             var doc = data.toString();
                             ProbingForDocument(clientid, instanceid, doc);
     
                         },
                         error: function (data) { alert("PDF ajax error"); },
                         dataType: 'json'
                     });
     
                 },
                 error: function (data) { alert("instance ajax error"); },
                 dataType: 'json'
             });
         },
         error: function (data) { alert("client ajax error"); },
         dataType: 'json'
     });
     
    function ProbingForDocument(cid, iid, did) {
         $.ajax({
             type: "GET",
             url: '/api/reports/clients/' + cid + '/instances/' + iid + '/documents/' + did + '/info',
             contentType: "application/json",
             data: {},
             success: function (data) {
                 if (data.documentReady != true) {
                     window.setTimeout(function () { ProbingForDocument(cid, iid, did); }, 500);
                 }
                 else {
     
                     location.href = '/api/reports/clients/' + cid + '/instances/' + iid + '/documents/' + did
                 }
             },
             error: function (data) { alert("Check PDF ready ajax error"); },
             dataType: 'json'
         });
     }
    To simplify the work with reports you can just change the passed report name (use TypeReportSource) and report parameters.

    On the question about changing the connection string based on the current user, as my colleague explained the recommended approach is to build a Data Access Layer, where you can get the authorized user information and load data based on it. The report can use an ObjectDataSource bound to that  DAL without any further modifications or custom IReportResolver implementation. The report will load only the data provided in the custom object. Additional parameters used for further data filtration can be passed through the report parameters(Using Parameters with the ObjectDataSource Component).

    If you have further questions, feel free to send us a sample project illustrating your settings and details about the selected approach.

    Regards,
    Stef
    Telerik

    New HTML5/JS REPORT VIEWER with MOBILE AND TOUCH SUPPORT available in Telerik Reporting Q3 2013! Get the new Reporting version from your account or download a trial.

Back to Top