Create a C# API Controller to launch SSRS Report 
Tags: ssrs, api, reports
Published: 2021-08-24 22:15:38

In this article I am going to build a C# API Controller to launch a SSRS Report using windows credentials and send back the response to client.

The first thing to do is know the URL of our SQL SSRS, 
for this sample let's suppose it is:
`http://devsqlserver/Reports/Payrol/CancelationReport`

Be aware this is not the web server URL, this is just the browser URL, here it's necessary to identify the folder name and the report name.

The way to know the webserver URL is look into the configuration of SSRS server. Regularly should look like this
`http://<SERVERNAME>/ReportServer`

So, in my case it is in fact `http://devsqlserver/ReportServer`

Here [launch an SSRS report in a browser](https://www.mssqltips.com/sqlservertip/3021/how-to-launch-an-ssrs-report-in-a-browser-window-from-a-net-application/) you can read a good article about this.

At this point I have the three data necessary to complete URL
`http://devsqlserver/ReportServer/Payrol/CancelationReport`

###Get report path using SQL query
If you have problems getting the correct report path, you could also use a simple select query 
```sql
select * from [reportserver].[catalog]
```
The result of this query will give you all the existing reports on the SSRS Server, so you can know the right path of every one.

Once I have the URL, it's now time to start working with the C# code of my controller

In this example I will assume the SSRS server is configured to accept windows credentials to launch reports

So if you already have created an empty API Controller, this is the segment code you need to know
###C# Fragment Code
```csharp
string ssrsServer = "http://devsqlserver/ReportServer?";
string rptname = "/Payrol/CancellationReport";
string param = "&rc:Parameters=false&rs:Command=Render&rs:Format=PDF&WarrantyPurchaseDateTo=6/02/2021";

try
  {
    Uri ssrsUrl = new Uri(ssrsServer + rptname + param);
    ICredentials credentials = CredentialCache.DefaultCredentials;
    NetworkCredential credential = credentials.GetCredential(ssrsUrl, "Basic");
                            
    WebClient client = new WebClient();
    client.Credentials = credential;
    //Read response
    var response = client.DownloadData(ssrsUrl);
    //Convert response into Base64 string
    var base64format = Convert.ToBase64String(response);
    return = base64format;
  }
```
Now it is time to explain the code fragment:
First I have the string variable that save the webserver, other variable to save the report path and the last one to save the parameters this report is going to use.

> Please take note of "?" after web server , this is mandatory

This variable that save the parameters also needs to save other information that tells SSRS how to render the report, in this case I using a PDF format, but it can also support some other formats like: XML, HTML4.0, EXCEL, WORD, CSV, RPL. 
You can find more detail information in this page [Export paginated reports](https://docs.microsoft.com/en-us/sql/reporting-services/report-builder/export-reports-report-builder-and-ssrs?view=sql-server-ver15)

Then just need to read the windows credentials and pass it to the Webclient.
```CSharp
ICredentials credentials = CredentialCache.DefaultCredentials;
NetworkCredential credential = credentials.GetCredential(ssrsUrl, "Basic");

WebClient client = new WebClient();
client.Credentials = credential;
```
If your URL is well formed and the credentials you send are accepted by SSRS webserver, taking note that expected format is PDF 

```CSharp
var response = client.DownloadData(ssrsUrl);
var base64format = Convert.ToBase64String(response);
```
The variable base64format saves a Base64 string that you could return to the client and transform that string into the correct file.