This is a migrated thread and some comments may be shown as answers.

How to set SubReports DataSource in Asp.net Core

1 Answer 262 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 1
Carlos asked on 19 Feb 2020, 01:37 PM

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;
                }
            }
        }
 
    }

1 Answer, 1 is accepted

Sort by
1
Carlos
Top achievements
Rank 1
answered on 19 Feb 2020, 04:19 PM

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;
                }
            }
        }
 
    }
Tags
General Discussions
Asked by
Carlos
Top achievements
Rank 1
Answers by
Carlos
Top achievements
Rank 1
Share this question
or