SQL Reporting Services 2005 – Print Report (Server-Side)

Before I begin this article I want to make sure it’s clear that I created this code a few years ago based on research I found on Google and MSDN (so I can’t take full credit for the implementation). This version of the code is customized and encapsulated in a standalone class. I believe this to be extremely useful for many developers out there.

 

If you’ve used SQL Server Reporting Services you know that printing a report programmatically isn’t as easy as calling ReportServer.Print(). This is why I put together a class that provides this functionality. The amount of code you have to write to print a report via the SQL Reporting Services Web Service can be a mind blowing task. Before we go any further, here is a link to the RSPrintManager class.

 

The first thing you’ll want to do is Add a Web Reference to your project or create a proxy class. The URL you should add a web reference to is as follows (replace <ReportServerName> with the name of you SQL Reporting Server):

 

·         http://<ReportServerName>/ReportServer/ReportExecution2005.asmx

 

Optionally, you can update the hardcoded URL so you don’t have to set it when you use the RSPrintManager:

private string m_url = “http://<ReportServerName>/ReportServer/ReportExecution2005.asmx”;

 

If you use the default constructor, you windows credentials will be used for Report Server Authentication. Otherwise, you can use the overloaded constructor to pass use different credentials. Let’s take a look at the use of this class:

 

// Create an instance of RSPrintManager w/the default windows credentials

RSPrintManager rsPintManager = new RSPrintManager();           

// this property doesn’t have to be set if you changed the hardcorded value of

// the “m_url” member in RSPrintManager

rsPintManager.Url = “http://<ReportServerName>/ReportServer/ReportExecution2005.asmx”;

// call the PrintReport method and pass the report path

rsPintManager.PrintReport(@“/CustomerReports/OpenWork”);

 

As you can see the use of this object is extremely easy to use. If your report contained parameters you’d write the code as follows:

// Create an instance of RSPrintManager w/the default windows credentials

RSPrintManager rsPintManager = new RSPrintManager();           

// this property doesn’t have to be set if you changed the hardcorded value of

// the “m_url” member in RSPrintManager

rsPintManager.Url = “http://<ReportServerName>/ReportServer/ReportExecution2005.asmx”;

// declare an array of ParameterValue objects based on the number of parameters in your report…

// in this case I have the parameters in a datagridview so I use the Count property to set the size

ReportExecution2005.ParameterValue[] @params = new ReportExecution2005.ParameterValue[DataGridView1.Rows.Count];

// Loop through each record in the datagridview and set the parameters accordingly

for (int i = 0; i <= @params.Length – 1; i++)

{

      @params[i] = new ReportExecution2005.ParameterValue();

      @params[i].Name = DataGridView1.Rows[i].Cells[“ParameterName”].Value.ToString();

      @params[i].Value = DataGridView1.Rows[i].Cells[“ParameterValue”].Value.ToString();

}

// call the PrintReport method and pass the report path

rsPintManager.PrintReport(@“/CustomerReports/OpenWork”, @params);

 

// Optionally you can use the overload to print a specific range of pages

// rsPintManager.PrintReport(@”/CustomerReports/OpenWork”, @params, 1, 5);

 

 

To get a list of parameters for a specific report  in the report server and populate a DataGridView, you’ll need to add a web reference or create a proxy class for the following URL:

 

·         http://<ReportServerName>/ReportServer/ReportService2005.asmx

 

 

// create an instance ReportingService2005  

ReportService2005.ReportingService2005 rs = new ReportService2005.ReportingService2005();

// set the URL

rs.Url = “http://<ReportServerName>/ReportServer/ReportService2005.asmx”;

// get a list of parameters

ReportService2005.ReportParameter[] items = rs.GetReportParameters(@“/CustomerReports/OpenWork”, null, false, null, null);

// Add the parameters to the DataGridView with blank values

foreach (ReportService2005.ReportParameter p in items)

{

      DataGridView1.Rows.Add(p.Name, “”);

}

Leave a Reply