Skip to content

Instantly share code, notes, and snippets.

@underwhelmed
Created July 5, 2011 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save underwhelmed/1065743 to your computer and use it in GitHub Desktop.
Save underwhelmed/1065743 to your computer and use it in GitHub Desktop.
Automatically run and email an SSRS Report
protected void btnRuns_Click(object sender, EventArgs e)
{
var rs = new rs2005.ReportingService2005();
var rsExec = new rsExecService.ReportExecutionService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;
rs.Url = "http://<ReportServer>/ReportServer/ReportService2005.asmx";
rsExec.Url = "http://<ReportServer>/ReportServer/ReportExecution2005.asmx";
string _reportName = @"/Path/To/Report";
string historyID = null;
string deviceInfo = null;
string format = "PDF";
Byte[] results;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
rsExecService.Warning[] warnings = null;
string[] streamIDs = null;
try
{
rsExecService.ExecutionInfo ei = rsExec.LoadReport(_reportName, historyID);
ICollection<rsExecService.ParameterValue> parameters = new List<rsExecService.ParameterValue>();
parameters.Add(AddParameter("UserID", "349562"));
rsExec.SetExecutionParameters(parameters.ToArray<rsExecService.ParameterValue>(), "en-us");
results = rsExec.Render(format, deviceInfo,
out extension, out encoding,
out mimeType, out warnings, out streamIDs);
using (MemoryStream stream = new MemoryStream(results))
{
Attachment attach = new Attachment(stream, "my_report_name.pdf");
MailMessage msg = new MailMessage();
msg.Attachments.Add(attach);
msg.To.Add(new MailAddress("someone@example.com"));
msg.From = new MailAddress("noreply@example.com");
SmtpClient client = new SmtpClient();
client.Send(msg);
}
}
catch (Exception ex)
{
throw ex;
}
}
private rsExecService.ParameterValue AddParameter(string name, string value)
{
return new rsExecService.ParameterValue() { Name = name, Value = value };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment