New to Telerik Reporting? Download free 30-day trial

Displaying Reports From Report Server through Custom ReportSource Resolver

Environment

Product Progress® Telerik® Reporting

Description

Sometimes you want to avoid adding the Report Server's credentials in the initialization of the viewer. This can be done by the usage of Custom Report Source Resolver.

Solution

In this scenario, you need to render the reports in a custom Reporting REST Service rather than in the Report Server. You will need a Custom ReportSource Resolver, in which to download the report definition from the server and return it wrapped in the proper server-side ReportSource.

  1. You need to add references to the Telerik.ReportServer.Services.Models and Telerik.ReportServer.HttpClient assemblies that are distributed with the Report Server assembly.
  2. In the Resolve method of the CustomReportSourceResolver, firstly, you should create a setting for the Report Server connection. Then based on the passed category and report name from the viewer, you need to get the report from the Report Server and return it as an InstanceReportSource:
public class CustomReportSourceResolver : IReportSourceResolver
{
    public Telerik.Reporting.ReportSource Resolve(string reportId, OperationOrigin operationOrigin, IDictionary<string, object> currentParameterValues)
    {
        var categoryAndReport = reportId.Split(new char[] { '-' });
        var categoryName = categoryAndReport[0];
        var reportName = categoryAndReport[1];

        var settings = new Telerik.ReportServer.HttpClient.Settings()
        {
            BaseAddress = "http://localhost:83/"
        };
        Telerik.Reporting.Report report = null;

        using (var rsClient = new ReportServerClient(settings))
        {
            rsClient.Login("myusername", "mypass");
            var categories = rsClient.GetCategories();
            var myCategoryId = categories.Where(item => item.Name == categoryName).First().Id;
            var reportInfos = rsClient.GetReportInfosInCategory(myCategoryId);
            var myReportId = reportInfos.Where(item => item.Name == reportName).First().Id;

            var reportDefinition = rsClient.GetLatestReportRevision(myReportId);
            var extension = reportDefinition.Extension;

            using (var sourceStream = new MemoryStream(reportDefinition.Content))
            {
                if (extension == "trdx")
                {
                    using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(sourceStream))
                    {
                        Telerik.Reporting.XmlSerialization.ReportXmlSerializer xmlSerializer =
                            new Telerik.Reporting.XmlSerialization.ReportXmlSerializer();

                        report = (Telerik.Reporting.Report)
                        xmlSerializer.Deserialize(xmlReader);
                    }
                }
                else
                {
                    var reportPackager = new ReportPackager();
                    report = (Report)reportPackager.UnpackageDocument(sourceStream);
                }
            }
        }
        InstanceReportSource rs = new InstanceReportSource() { ReportDocument = report };
        return rs;
    }
}

See Also

In this article