Edit this page

Export Report in Silverlight

Environment

Product Progress® Telerik® Reporting
Project Type Silverlight Application

Description

In the following article are presented steps for exporting a report in Silverlight application by accessing the report engine via API.

Solution

To export a Telerik report from a Silverlight application without a Silverligh Report Viewer you have to instantiate the Telerik.Reporting.Service.SilverlightClient.ReportServiceClient class, which plays the role of proxy to the Telerik Reporting WCF Service.

To specify that you want to render the report we use the ReportServiceClient.RenderAsync method of the ReportServiceClient, which invokes the respective method on the server (remember that it serves as proxy) that would return the rendered report.

Since we’ve used an asynchronous method and we do not know when the render would finish, we need to handle the ReportServiceClient.RenderCompleted event. In the RenderCompleted event we get the result from the rendering and decide what to do with it - in this case write down the report.

The ShowDialog method is invoked prior to having the rendered report, because the dialog box, can only be called from user-initiated code, such as a button Click event. If ShowDialog is called from code that is not user-initiated, a SecurityException is thrown.

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }

    Stream file;

    private void Button_Click(object sender, RoutedEventArgs e)
    { 
        var fileDlg = new SaveFileDialog();
        fileDlg.Filter = "PDF files|*.pdf|All files|*.*";

        if (fileDlg.ShowDialog() == true)
        {
            //set any deviceInfo settings if necessary
            var deviceInfo = new NameValueDictionary();
            //set any report parameter value if necessary
            var parameters = new NameValueDictionary();

            this.file = fileDlg.OpenFile();
            var serviceClient = new ReportServiceClient(new Uri(App.Current.Host.Source, "../ReportService.svc"));
            serviceClient.RenderAsync("PDF", 
                "ClassLibrary1.Report1, ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", 
                deviceInfo, 
                parameters);
            serviceClient.RenderCompleted += new EventHandler<RenderEventArgs>(serviceClient_RenderCompleted);
        }
    }

    void serviceClient_RenderCompleted(object sender, RenderEventArgs e)
    {
        var result = e.RenderingResult;
        if (this.file != null)
        {
            this.file.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
            this.file.Close();
            this.file.Dispose();
            this.file = null;
        }
    }
}

See Also

Telerik.Reporting.Service.SilverlightClient.ReportServiceClient

Is this article helpful? Yes / No
Thank you for your feedback!

Give article feedback

Tell us how we can improve this article

close
Dummy