How to set SubReports DataSource in Asp.net Core

2 posts, 0 answers
  1. Carlos
    Carlos avatar
    19 posts
    Member since:
    Jun 2019

    Posted 19 Feb Link to this post

    Here is my problem.  Our shop has three environments that the development team must work with, development, testing and production.  I have successfully been able to change the DataSource (WebServiceDataSource) of the master report by changing the ServiceUrl.  For subreports I have been able to change the ReportSource based on the environment but I have not been able to locate a way to change the DataSource.  Does anyone have an idea how this is done? 

    Here is the code I have right now:

    public class TexasProtaxCustomReportResolver : Telerik.Reporting.Services.Engine.IReportResolver
        {
            private readonly string _path;
            private readonly string _apiUrl;
     
            public TexasProtaxCustomReportResolver(string path, string apiUrl)
            {
                _path = path;
                _apiUrl = apiUrl;
            }
     
            public Telerik.Reporting.ReportSource Resolve(string reportName)
            {
                var reportPacker = new ReportPackager();
     
                string reportPath = Path.Combine(_path, reportName);
                using (var sourceStream = System.IO.File.OpenRead( reportPath))
                {
                    var report = (Telerik.Reporting.Report)reportPacker.UnpackageDocument(sourceStream);
                    SetReportSource((ReportItemBase)report);
     
                    var irs = new InstanceReportSource
                    {
                        ReportDocument = report
                    };
     
                    return irs;
                }
     
            }
     
           private void SetReportSource(ReportItemBase reportItemBase)
            {
                if (reportItemBase.Items.Count < 1)
                    return;
     
                if (reportItemBase is Telerik.Reporting.Report)
                {
                    var report = (Telerik.Reporting.Report)reportItemBase;
     
                    var webServiceDataSource = (WebServiceDataSource)report.DataSource;
                    string url = webServiceDataSource.ServiceUrl;
                    url = url.ToLower().Replace("localhost:5001", _apiUrl);
                    webServiceDataSource.ServiceUrl = url;
                }
     
                foreach (var item in reportItemBase.Items)
                {
                    // recursively set the reportsource
                    SetReportSource(item);
     
                    if (item is SubReport subReport)
                    {
                        // Save Parameters
                        var clonable = subReport.ReportSource.Parameters as ICloneable;
                        var parameters = (ParameterCollection)clonable.Clone();
     
                        string fileName = Path.GetFileName(subReport.ReportSource.ToString());
                        var uriReportSource = new UriReportSource
                        {
                            Uri = Path.Combine(_path, fileName)
                        };
                        uriReportSource.Parameters.AddRange(parameters);
     
                        subReport.ReportSource = uriReportSource;
     
                        continue;
                    }
                }
            }
     
        }
  2. Carlos
    Carlos avatar
    19 posts
    Member since:
    Jun 2019

    Posted 19 Feb Link to this post

    Every time I post a question I figure it out.  Instead of a UriReportSource I used an XmlReportSource which I can get to the report object.  Code follows for those interested.

    public class TexasProtaxCustomReportResolver : Telerik.Reporting.Services.Engine.IReportResolver
        {
            private readonly string _path;
            private readonly string _apiUrl;
     
            public TexasProtaxCustomReportResolver(string path, string apiUrl)
            {
                _path = path;
                _apiUrl = apiUrl;
            }
     
            public Telerik.Reporting.ReportSource Resolve(string reportName)
            {
                var reportPacker = new ReportPackager();
     
                string reportPath = Path.Combine(_path, reportName);
                using (var sourceStream = System.IO.File.OpenRead( reportPath))
                {
                    var report = (Telerik.Reporting.Report)reportPacker.UnpackageDocument(sourceStream);
                    SetReportSource((ReportItemBase)report);
     
                    var irs = new InstanceReportSource
                    {
                        ReportDocument = report
                    };
     
                    return irs;
                }
     
            }
     
           private void SetReportSource(ReportItemBase reportItemBase)
            {
                if (reportItemBase.Items.Count < 1)
                    return;
     
                if (reportItemBase is Telerik.Reporting.Report)
                {
                    var report = (Telerik.Reporting.Report)reportItemBase;
     
                    var webServiceDataSource = (WebServiceDataSource)report.DataSource;
                    string url = webServiceDataSource.ServiceUrl;
                    url = url.ToLower().Replace("localhost:5001", _apiUrl);
                    webServiceDataSource.ServiceUrl = url;
                }
     
                foreach (var item in reportItemBase.Items)
                {
                    // recursively set the reportsource
                    SetReportSource(item);
     
                    if (item is SubReport subReport)
                    {
                        // Save Parameters
                        var clonable = subReport.ReportSource.Parameters as ICloneable;
                        var parameters = (ParameterCollection)clonable.Clone();
                        string fileName = Path.GetFileName(subReport.ReportSource.ToString());
                        string subReportPath = Path.Combine(_path, fileName);
     
                        var reportPacker = new ReportPackager();
     
                        string xml;
                        using (var sourceStream = System.IO.File.OpenRead(subReportPath))
                        {
                             
                            var report = (Telerik.Reporting.Report)reportPacker.UnpackageDocument(sourceStream);
     
                            var webServiceDataSource = (WebServiceDataSource)report.DataSource;
                            string url = webServiceDataSource.ServiceUrl;
                            url = url.ToLower().Replace("localhost:5001", _apiUrl);
                            webServiceDataSource.ServiceUrl = url;
     
                            using (StringWriter writer = new StringWriter())
                            {
                                ReportXmlSerializer serializer = new ReportXmlSerializer();
                                serializer.Serialize(writer, report);
                                xml = writer.ToString();
                            }
                             
                        }
     
                        var xmlReportSource = new XmlReportSource
                        {
                            Xml = xml
                        };
                        xmlReportSource.Parameters.AddRange(parameters);
                        subReport.ReportSource = xmlReportSource;
     
                        continue;
                    }
                }
            }
     
        }
Back to Top