Download SQL Server Reporting Service (SSRS) report as PDF using C#
SQL Server Reporting service is a server based reporting service from Microsoft. SSRS reports are widely used in generating complex reports in simple ways and it competes with Crystal Reports and other business intelligence tools. There are cases and requirements where we need to automatically download SSRS report as PFD and other formats and process it with a .Net application. The below article explains the steps and a sample code to download SSRS report as PDF using c# and save it to hard disk.
Steps to download SSRS report as PDF using C#
- Add Web Reference to the project by connecting to the Report Server
- Create an object instance to ReportExecutionService by setting the Credentials and SSRS report server URL
- Initialize the report parameters to be passed
- Execute the Render(..) method to download the report as bytes
- Write the received bytes as file to hard disk
Adding Web Reference of Report Server to Visual Studio Solution
If we need to add an external library to the project, we need to either add the Dynamic-Link Library (dll) as reference or download the library via NuGet Package. In case of downloading the SSRS report as PDF using C#, the library has to generated by connecting to our Report Server. We do not have any other source where we can download the library.
Microsoft SQL Server Reporting Service provides an SOAP end-point service over HTTP to which we can connect and generate the necessary namespaces and library. Visual Studio does generating the library automatically once we provide the correct service URL.
Every Reporting Server provides an SOAP end-point to ReportExecution2005. You can check if the end-point works correctly by navigating to the below URL in your browser. Replace <<server>> with your server name. More details on the SOAP end-point can be found at https://docs.microsoft.com/en-us/sql/reporting-services/report-server-web-service/accessing-the-soap-api?view=sql-server-2017
http://<<server>>/reportserver/reportexecution2005.asmx
The end-point ReportExecution2005 is common to all SSRS Report server 2005 and above. Even if your SSRS report server version is 2010/2012 or above it has the end-point ReportExecution2005
Adding SQL Server Reporting Service Web Reference to Project
Download SSRS report as PDF using C#
using DownloadSSRSReport.craftedforeveryone.com; using System.Collections.Generic; using System.IO; using System.Net; namespace DownloadSSRSReport { class Program { static void Main(string[] args) { ReportExecutionService rs = new ReportExecutionService(); rs.Credentials = CredentialCache.DefaultCredentials; rs.Url = "http://craftedforeveryone.com/reportserver/reportexecution2005.asmx"; rs.ExecutionHeaderValue = new ExecutionHeader(); var executionInfo = new ExecutionInfo(); executionInfo = rs.LoadReport("/your report path/report name", null); List<ParameterValue> parameters = new List<ParameterValue>(); parameters.Add(new ParameterValue { Name = "parameterId", Value = "value" }); rs.SetExecutionParameters(parameters.ToArray(), "en-US"); string deviceInfo = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>"; string mimeType; string encoding; string[] streamId; Warning[] warning; var result = rs.Render("PDF", deviceInfo, out mimeType, out encoding, out encoding, out warning, out streamId); File.WriteAllBytes("c:tempreport.pdf", result); } } }
Common Queries and Errors
Type or namespace “ReportExecutionService” could not be found
Add Web Reference to the project with URL destination as http://<<server>> /reportserver/reportexecution2005.asmx. Replace <<server>> with your reporting server URL.
I have already added the web reference to project but still “ReportExecutionService” is not found
Make sure you have added Web Reference to ReportExecution2005 end-point and not ReportService2005
Library or NuGet package not found for "ReportExecutionService"
There is no separate library or NuGet package available for ReportExecutionService. Visual studio will automatically generate the required library files once after adding the Web Reference to the project
ReportService2010.asmx or above added to the project but ReportExecutionService is not available.
Even though you have added the latest version of ReportService you need to add reference to ReportExecution2005. The rendering services are available only as part of ReportExecution2005 service.
How do I pass multiple choice / multi select input parameter to download the SSRS report using C#
Initialize ParameterValue object multiple times with same Name (Parameter ID) but with different value. example
List<ParameterValue> parameters = new List<ParameterValue>(); parameters.Add(new ParameterValue { Name = "StudentId", Value = "Student 1" }); parameters.Add(new ParameterValue { Name = "StudentId", Value = "Student 2" });
i used vs 2010. How to find the report path?? Exception occurs. Please help
The path of the item ‘WSR’ is not valid. The full path must be less than 260 characters long; other restrictions apply. If the report server is in native mode, the path must start with slash. —> Microsoft.ReportingServices.Diagnostics.Utilities.InvalidItemPathException: The path of the item ‘WSR’ is not valid. The full path must be less than 260 characters long; other restrictions apply. If the report server is in native mode, the path must start with slash.
please confirm if We can skip ParameterValue if we don’t have any input parameter for report?