sounds simple dosen’t it – my objective is to run an estimate report and render to PDF all from the click of a button as shown in the screenshot below:

Heres the code –

string oppID = Request.QueryString["oppID"].ToString();
ReportingService rs = new ReportingService();
rs.Url = "http://server/reportserver/ReportService.asmx";
System.Net.NetworkCredential creds = new System.Net.NetworkCredential();
creds.Domain = "domain";
creds.UserName = "username";
creds.Password = "password";
rs.Credentials = creds;
byte[] result = null;
string reportPath = "/CRMORG_MSCRM/4.0/{c6352760-a801-e011-9a31-005056ad3d4b}";
string format = "PDF";
string historyID = null;
string devInfo = @"False";
string showHideToggle = null;
string encoding;
string mimeType;
string extension;
ParameterValue[] parameterUsed;
string[] streamIds;
Warning[] warnings = null;
ParameterValue[] parameters = new ParameterValue[1];
parameters[0] = new ParameterValue();
parameters[0].Name = "OpportunityID"; // parameter name
parameters[0].Value = "{C7DF8F1C-8DF6-DF11-9A31-005056AD3D4B}";
DataSourceCredentials dsc = new DataSourceCredentials();
dsc.DataSourceName = "DataSource1";
dsc.UserName = "C9444B29-E6FC-DF11-9A31-005056AD3D4B"; // SystemuserId from the filteredUser table
dsc.Password = "D945754F-C1DF-DF11-81ED-005056AD3D4B"; // OrganizationId from the filteredUser table
DataSourceCredentials[] credentials = new DataSourceCredentials[] { dsc };
result = rs.Render(reportPath, format, historyID, devInfo, parameters, credentials, showHideToggle, out encoding, out mimeType, out parameterUsed, out warnings, out streamIds);
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + Request.QueryString["oppID"].ToString() + ".pdf");
Response.BufferOutput = true;
Response.BinaryWrite(result);
Response.End();
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.Flush();
Response.Close();

Deploy aspx to CRM ISV folder — create ISV button to call the ASPX page passing the oppID parameter (being the actual opportunityid) to the page.