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.
Follow these steps to build and run the
example.
-
Launch Visual Studio 2005.
-
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.
-
Open the form and give it a meaningful
caption
. Right-click
Form1.cs
in the Solution Explorer pane and
select Open from the context menu. Set its Text property =
AdventureWorks Sample
Reports
.
-
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.
-
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.
-
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.
-
Click the
ReportService2005
link to display a dialog
describing the Web service.
-
In the
"ReportingService2005" Description
dialog, set
the
Web reference name
=
RS2005
as shown in the completed
dialog shown in the following figure.
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.
-
Follow steps 5 through 8 to add reference to the
ReportExecution2005
servicename
the reference
RE2005
.
-
Open the code for
Form1
. Right-click
Form1.cs
in the Solution Explorer pane and
select View Code from the context menu.
-
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;
-
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);
}
-
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);
}
}
}
}
}
}
-
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.