Elsimer
Elsimer

Reputation: 1888

How do I customize the name of a report in SSRS 2016 to be output?

I want to export a paginated report from SSRS 2016 with a custom name from code. I can do it if I use the old 2005 control, but when I use the new URL access it either prompts me (if I'm in Edge) but defaults to the name of the report or in Chrome it just downloads with no prompting. The report name, for example, is MyTestReport with several parameters passed to it. I'd like to be able to use those parameters to set the filename.

http://localhost/ReportServer/Pages/ReportViewer.aspx?/Finance/MyTestReport&i_entityID=98&i_sy=2016&i_runType=3&i_isPreview=true&rs:Format=PDF

That URL would ideally create a filename of 2016_Preview_Report.pdf if possible. I've searched through the SSRS documentation but can't find anything on how to go about doing so. A section of the code I'm using to export to PDF using the old 2005 control is below:

IReportServerCredentials irsc = new CustomReportCredentials(userid, password, domain);
var parametersCollection = new List<ReportParameter>();
parametersCollection.Add(new ReportParameter("i_sy", SY.ToString(), false));
parametersCollection.Add(new ReportParameter("i_entityID", LEA.ToString(), false));
parametersCollection.Add(new ReportParameter("i_runType", runtype.ToString(), false));
parametersCollection.Add(new ReportParameter("i_isPreview", isPreview.ToString(), false)); 
ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
rv.ProcessingMode = ProcessingMode.Remote;
rv.ServerReport.ReportServerCredentials = irsc;
rv.ServerReport.ReportPath = REPORT_PATH;
rv.ServerReport.ReportServerUrl = new Uri(SSRS_REPORT_SERVER);
rv.ServerReport.SetParameters(parametersCollection);
rv.ServerReport.Refresh();
byte[] streamBytes = null;
string mimeType = "";
string encoding = "";
string filenameExtension = "PDF";
string[] streamids = null;
Warning[] warnings = null;
FileContentResult ReturnFile = null;
filenameExtension = ExportType;
mimeType = "application/pdf";
streamBytes = rv.ServerReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
ReturnFile = File(streamBytes, mimeType, filename + "."+filenameExtension);
return ReturnFile;

Upvotes: 1

Views: 1485

Answers (1)

Rambling Geek
Rambling Geek

Reputation: 33

This is how I did it:

  public ActionResult GetPDFReport(int? id)
{
   string filename = "Generate Filename Here"

   NetworkCredential nwc = new NetworkCredential("username", "password");
   WebClient client = new WebClient();
   client.Credentials = nwc;

   string reportURL = "http://servername//ReportServer?PO&rs:Format=PDF&rs:ClearSession=true&Param1=" + id;
   return File(client.DownloadData(reportURL), "application/pdf", filename);
}

Upvotes: 2

Related Questions