Section 8.3. Web Services


8.3. Web Services

Reporting Services provides full access to report server functionality through web services. The web service provides methods and properties for both report executioncontrolling the processing and rendering of reportsand report management.

The example in this section accesses a report from a .NET applicationyou can use any development tool that is capable of invoking SOAP methods.

This example builds a Windows application that retrieves the list of reports in the Adventure Works Sample Reports folder and displays them in a list box. The user can render a report and save it to a Web archive (.mht) file. The complete, running application is shown in the following figure.

Figure 8-2. Completed Application (Running)


Follow these steps to build and run the example.

  1. Launch Visual Studio 2005.

  2. Create a new C# Windows application. Select File New Project from the main menu. Select Visual C# Windows project type, and the Windows Application template. Name the application SS2005RE WebService. Press OK to create the application.

  3. Add a list of reports and a button to render the report selected in the list. Drop a ListBox control onto Form1 and name it reportListBox. Drop a Button control onto Form1, set its Name property = renderButton, and set its Text property = Render.

    The form is shown in the following figure.

    Figure 8-3. Completed Form1 Design

  4. Right-click on References in the Solution Explorer pane and select Add Web Reference... from the context menu to display the Add Web ReferenceStart Browsing for Web Services dialog as shown in the following figure.

    Figure 8-4. Add Web Reference Dialog

  5. Click on the Web services on the local machine link to display a dialog listing Web Services on the local machine as shown in the following figure.

    The ReportService2005 Web service is used to programmatically manage reporting objects. The ReportExecution2005 Web service is used to programmatically process and render reports.

  6. Click the ReportService2005 link to display a dialog describing the Web service.

  7. In the "ReportingService2005" Description dialog, set the Web reference name = RS2005 as shown in the completed dialog shown in the following figure.

    Figure 8-5. ReportingService2005 Description Dialog

    Click the Add Reference button to add the Web service reference to the project. The Web References node in the Solution Explorer pane will update to show the new Web service reference.

  8. Follow steps 5 through 8 to add reference to the ReportExecution2005 servicename the reference RE2005.

  9. Open the code for Form1. Right-click Form1.cs in the Solution Explorer pane and select View Code from the context menu.

  10. Add using directives to the top of the class for the ReportService2005 and ReportExecution2005 Web services:

     using SS2005RE_WebService.RE2005; using SS2005RE_WebService.RS2005; 

    Add a using directive to the top of the class for IO:

     using System.IO; 

  11. Add an event handler to populate the list box when the form is created. Switch to the form designer view. Double-click on the form (but not on the list box or button) to add an event handler for the Load event. Switch back to the code view and add the following code for the Form1_Load method:

     // Instantiate Web service proxy used to programmatically // report objects ReportingService2005 rs = new ReportingService2005(); rs.Credentials = System.Net.CredentialCache.DefaultCredentials; // Get reports in the folder CatalogItem[] cis = rs.ListChildren(@"/AdventureWorks Sample Reports",       false); foreach (CatalogItem ci in cis) {       // Populate list box with Report items       if (ci.Type == ItemTypeEnum.Report)             reportListBox.Items.Add(ci.Name); } 

  12. Add an event handler to render and save the report when the Render button is clicked. Switch to the form designer view. Double-click on the button renderButton to add an event handler for the load event. Add code to the renderButton_Click method to render the report to a user-specified file.

    The complete code for Form1.cs, including the renderButton_Click event handling code, follows:

     using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using SS2005RE_WebService.RE2005; using SS2005RE_WebService.RS2005; namespace SS2005RE_WebService {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         private void Form1_Load(object sender, EventArgs e)         {             // Instantiate Web service proxy used to programmatically             // manage reporting objects             ReportingService2005 rs = new ReportingService2005();             rs.Credentials =                   System.Net.CredentialCache.DefaultCredentials;             // Get reports in the folder             CatalogItem[] cis = rs.ListChildren(                   @"/AdventureWorks Sample Reports", false);             foreach (CatalogItem ci in cis)             {                 // Populate list box with Report items                 if (ci.Type == ItemTypeEnum.Report)                     reportListBox.Items.Add(ci.Name);             }         }         private void renderButton_Click(object sender, EventArgs e)         {             if (reportListBox.SelectedIndex >= 0)             {                 // Get the name of the report selected in the list box                 string reportName = reportListBox.Items                   [reportListBox.SelectedIndex].ToString();                 // Get the name to save the report to                 SaveFileDialog sfd = new SaveFileDialog();                 sfd.Filter = "Web archive (*.mht)|*.mht";                 sfd.FileName = reportName;                 if (sfd.ShowDialog() == DialogResult.OK)                 {                     string renderFileName = sfd.FileName;                     // Instantiate Web service proxy used to                     // programmatically process and render reports                     ReportExecutionService re =                         new ReportExecutionService();                     re.Credentials =                         System.Net.CredentialCache.DefaultCredentials;                     // byte array to hold rendered report                     byte[] report;                     // report rendering argument                     string reportFullName =                        @"/AdventureWorks Sample Reports" + "/" +                        reportName;                     string format = "MHTML";                     string historyID = null;                     string deviceInfo = null;                     RE2005.ParameterValue[] pv =                         new RE2005.ParameterValue[0];                     string encoding;                     string mimeType;                     string extension;                     RE2005.Warning[] warning = null;                     string[] streamIDs = null;                     try                     {                         // load report, set parameters, and render                         re.LoadReport(reportFullName, historyID);                         re.SetExecutionParameters(pv, "en-us");                         report = re.Render(format, deviceInfo,                               out extension, out mimeType,                               out encoding, out warning,                               out streamIDs);                         // save report to user-specified file                         using (FileStream fs =                               File.OpenWrite(renderFileName))                             fs.Write(report, 0, report.Length);                     }                     catch (Exception ex)                     {                         MessageBox.Show(ex.Message, "Error",                             MessageBoxButtons.OK,                             MessageBoxIcon.Error);                     }                 }             }         }     } } 

  13. Compile and run the report. Select the Company Sales report and click the Render button. Accept the default file name Company Sales. Close the application. Open the file to view the saved report as shown in the following figure.

    Figure 8-6. Saved Company Sales Report Company Sales.mht




SQL Server 2005 Reporting Essentials
SQL Server 2005 Reporting Essentials
ISBN: 735617880
EAN: N/A
Year: 2007
Pages: 31

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net