Introduction
This tip demonstrates how to access SSRS Reports stored on SharePoint site using C#. It primarily focuses on demonstrating this using a Desktop application that can be developed using Windows Forms / WPF.
In this article, we will learn how to :
- Display the SSRS report using the ReportViewer control in .NET.
- Convert the SSRS Report to PDF Attachment that can be used by the SMTPClient object in .NET to send this report as an Email Attachment (PDF).
- Save the report as PDF.
Background
Microsoft Reports can be designed using two different methods - as a Local Report and as a Server Report. For designing the local report, the report DataSet (.XSD file) has to be a part of the .NET Project which is using the report. In case of any changes to the design or data presented by the report, the entire application needs to be re-compiled and re-deployed.
However, if the report is developed as a Server Report, this problem can be eliminated. We will be storing the SSRS report (built locally) on the SharePoint site, and our Desktop Application built using WPF will be accessing this Server Report using C#.
Using the Code
In our example, we have a class named SSRSReport
, which has different methods to perform the tasks mentioned in the Introduction section of this article.
public class SSRSReport
{
private static String GetReportServerURL()
{
DataTable datatable = new DataTable();
DBConnect.FillDataTable("GetSSRSReportServerURL", datatable, null);
if (datatable == null || datatable.Rows.Count == 0)
return null;
else
return datatable.Rows[0]["PARAMETER_VALUE"].ToString().Trim();
}
public static void DisplayReport(ReportViewer reportViewer, String reportPath)
{
try
{
reportViewer.ProcessingMode = ProcessingMode.Remote;
ServerReport serverreport = reportViewer.ServerReport;
ICredentials credentials = CredentialCache.DefaultCredentials;
ReportServerCredentials rscredentials = serverreport.ReportServerCredentials;
rscredentials.NetworkCredentials = credentials;
serverreport.ReportServerUrl = new Uri(GetReportServerURL());
serverreport.ReportPath = reportPath;
reportViewer.ShowParameterPrompts = false;
reportViewer.ShowPrintButton = true;
reportViewer.Refresh();
reportViewer.SetDisplayMode(DisplayMode.PrintLayout);
}
catch (Exception)
{
throw;
}
}
public static void DisplayReport(ReportViewer reportViewer,
String reportPath, List<ReportParameter> reportParameterList)
{
try
{
reportViewer.ProcessingMode = ProcessingMode.Remote;
ServerReport serverreport = reportViewer.ServerReport;
ICredentials credentials = CredentialCache.DefaultCredentials;
ReportServerCredentials rscredentials = serverreport.ReportServerCredentials;
rscredentials.NetworkCredentials = credentials;
serverreport.ReportServerUrl = new Uri(GetReportServerURL());
serverreport.ReportPath = reportPath;
reportViewer.ShowParameterPrompts = false;
reportViewer.ShowPrintButton = true;
if (reportParameterList != null)
{
foreach (ReportParameter param in reportParameterList)
{
serverreport.SetParameters(param);
}
}
reportViewer.Refresh();
reportViewer.SetDisplayMode(DisplayMode.PrintLayout);
}
catch (Exception)
{
throw;
}
}
public static Attachment ConvertToPDFAttachment(ReportViewer reportViewer, String fileName)
{
try
{
byte[] data;
if (reportViewer.ServerReport != null)
data = reportViewer.ServerReport.Render("PDF");
else
data = reportViewer.LocalReport.Render("PDF");
Attachment att = new Attachment(new MemoryStream(data), fileName);
return att;
}
catch (Exception)
{
throw;
}
}
public static Boolean SaveAsPDF(ReportViewer reportViewer, String filePath)
{
try
{
byte[] data;
if (reportViewer.ServerReport != null)
data = reportViewer.ServerReport.Render("PDF");
else
data = reportViewer.LocalReport.Render("PDF");
FileStream fs = new FileStream(filePath, FileMode.Create);
fs.Write(data, 0, data.Length);
fs.Close();
return true;
}
catch(Exception)
{
throw;
}
}
public static Boolean SaveAsPDF(ReportViewer reportViewer, String filePath, String reportPath)
{
try
{
DisplayReport(reportViewer, reportPath);
byte[] data;
if (reportViewer.ServerReport != null)
data = reportViewer.ServerReport.Render("PDF");
else
data = reportViewer.LocalReport.Render("PDF");
FileStream fs = new FileStream(filePath, FileMode.Create);
fs.Write(data, 0, data.Length);
fs.Close();
return true;
}
catch (Exception)
{
throw;
}
}
public static Boolean SaveAsPDF(ReportViewer reportViewer,
String filePath, String reportPath, List<ReportParameter> reportParameterList)
{
try
{
DisplayReport(reportViewer, reportPath,reportParameterList);
byte[] data;
if (reportViewer.ServerReport != null)
data = reportViewer.ServerReport.Render("PDF");
else
data = reportViewer.LocalReport.Render("PDF");
FileStream fs = new FileStream(filePath, FileMode.Create);
fs.Write(data, 0, data.Length);
fs.Close();
return true;
}
catch (Exception)
{
throw;
}
}
}
Now we will see an example of how this class can be used:
public class SSRSReportViewer
{
private ReportViewer _reportViewer;
SSRSReport.DisplayReport(_reportViewer, ReportPath);
List<ReportParameter> paramList = new List<ReportParameter>();
paramList.Add(new ReportParameter("param1", param1);
paramList.Add(new ReportParameter("param2", param2);
SSRSReport.DisplayReport(_reportViewer, ReportPath ,paramList);
Attachment att = SSRSReport.ConvertToPDFAttachment(this._reportViewer, fileName);
SSRSReport.SaveAsPDF(_reportViewer, filePath);
SSRSReport.SaveAsPDF(_reportViewer, filePath, ReportPath);
SSRSReport.SaveAsPDF(_reportViewer, filePath, ReportPath, paramList);
}
Points of Interest
Nothing discovered so far. It seems a pretty straightforward process.
History
This is the first version of the DLL. There will be more updates as we come across bugs and/or additional functionality.