Integrating Reporting Services (SSRS) 2008 into Silverlight applications

Task of the day: create SSRS reports that can be shared between desktop (WPF) and Silverlight applications.

Silverlight does not have SSRS report viewer.  So what should we do?  The cleanest (and easiest) solution is to utilize ASP.NET capabilities.  ASP.NET does have web based controls for viewing SSRS reports.  So, we will create an aspx page in the web application that hosts Silverlight application.  We will set it up as follows:

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<html xmlns="http://www.w3.org/1999/xhtml" style="height:100%;">
<head runat="server">
    <title>Report</title>
</head>
<body>
    <form id="form1" runat="server" style="height:100%" >
    <div style="height:100%" >
        <rsweb:ReportViewer ID="MainReportViewer" runat="server" 
            ProcessingMode="Remote"
            Width="95%"
            Height="700"
            ShowExportControls="True"
            ShowFindControls="True"
            ShowParameterPrompts="False"
            ShowPromptAreaButton="False"
            ShowRefreshButton="False"
            BackColor="White">
            <ServerReport DisplayName="MainReport" ReportServerUrl="" />
        </rsweb:ReportViewer>
    </div>
    </form>
</body>
</html>

Now the next step is to make sure this web page can parse query parameters and show appropriate report:

protected void Page_Load(object sender, EventArgs e)
        {
            MainReportViewer.ProcessingMode = ProcessingMode.Remote;
            MainReportViewer.ServerReport.ReportPath = "/SSRSReports/" + this.Request.QueryString["ReportName"];
            MainReportViewer.ServerReport.ReportServerUrl = new Uri("http://localhost:8080/ReportServer_SQL2008");
            List<ReportParameter> parameters = new List<ReportParameter>();
            List<string> values = new List<string>();
            values.Add("1");
            values.Add("2");
            values.Add("3");
            ReportParameter oneParamter = new ReportParameter("MyParameter", values.ToArray());
            parameters.Add(oneParamter);
            MainReportViewer.ServerReport.SetParameters(parameters.ToArray());
            MainReportViewer.ShowParameterPrompts = false;
            MainReportViewer.ServerReport.Refresh();
        }

Code above contains hard-coded report parameters.  But, as you can see those can be easily passed in as part of query string as well.

Now, code from Silverlight application:

private void Button_Click(object sender, RoutedEventArgs e)
    {

        Uri sourceUri = new Uri(HtmlPage.Document.DocumentUri, Application.Current.Host.Source.ToString().Substring(0, Application.Current.Host.Source.ToString().IndexOf("ClientBin") – 1) + "/ReportViewerForm.aspx?ReportName=DevelopmentPlanAction");

        if (true == HtmlPage.IsPopupWindowAllowed)
        {
            System.Text.StringBuilder codeToRun = new System.Text.StringBuilder();
            codeToRun.Append("window.open(");
            codeToRun.Append(""");
            codeToRun.Append(sourceUri.ToString());
            codeToRun.Append("",");
            codeToRun.Append(""");
            codeToRun.Append("",");
            codeToRun.Append(""");
            codeToRun.Append("width=1000,height=900,scrollbars=yes,menubar=no,toolbar=no,resizable=yes");
            codeToRun.Append("");");
            try
            {
                HtmlPage.Window.Eval(codeToRun.ToString());
            }
            catch
            {
                MessageBox.Show("You must enable popups to view reports.  Safari browser is not supported.", "Error", MessageBoxButton.OK);
            }
        }
        else
            MessageBox.Show("You must enable popups to view reports.  Safari browser is not supported.", "Error", MessageBoxButton.OK);
    }

This code simply creates a javascript that is using the same web URL, but adds the address to our report viewer web page we just created.  Ideally, we should have use  built-in Silverlight function HtmlPage.PopupWindow, but it does not honor options, always creating non-resizeable browser window.  So, I am resorting to javascript instead that does create a resizeable window, but without menu, etc…  The only sad part is that Siliverlight application does launch another window, but there is no way to workaround this issue.  Overall I am pretty happy with this solution.  What is also pretty cool is that the same SSRS report can be called from WPF application.  In that case we use WinForm SSRS viewer that we host inside WindowsFormsHost  control like so:

<winForms:WindowsFormsHost x:Name="ViwerHost" Grid.Row="1" Background="White">
            <reports:ReportViewer x:Name="ReportViewerControl">
            </reports:ReportViewer>
        </winForms:WindowsFormsHost>

The code to launch the report is pretty much identical to the one for ASP.NET

Cool, hah?

3 Comments

  1. Hey Guys. I know this is an old article but I need some help on this.
    When I add parameters on the report and I run the report from ReportBuilder3 the report works fine, but when I run the report the way mentioned in this article, changing the report parameters does nothing. The report does not refresh. So I have a combo and I change the value and then click on the view report button and it makes no difference. I did change the parameter advanced settings to refresh the report when changing the parameters but still the problem remains. Any Idea how to resolve this issue?
    Thanks in Advance.

  2. You need to double check entire process. Make sure that the report is actually paying attention to parameter values, and trace / debug entire process to make sure that correct values are passed through the entire use flow.

Leave a Reply to Sergey Barskiy Cancel reply

Your email address will not be published. Required fields are marked *