web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Sample Code to use ReportExecution2005.asmx to generate PDF in CRM 2011.

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee

Hi,

Just sharing a sample code that uses ReportExecution2005 web service of SSRS 2008, and generates pdf of one of the custom reports deployed in CRM.

Here we are also passing value to one of the report parameter.

 // Add web reference to following service
 // http://server/reportserver/ReportExecution2005.asmx

ReportExecutionService rs = new ReportExecutionService();
 rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Render arguments
 byte[] result = null;
 // Get the report path from the report server
 // http://reportserver/Reports/Pages/Folder.aspx?ViewMode=Detail

string reportPath = "/MyOrg_MSCRM/CustomReports/{837a8e7e-d949-e111-996a-00155d2a49c7}";
 string format = "PDF";
 string historyID = null;

string encoding;
 string mimeType;
 string extension;
 Warning[] warnings = null;
 string[] streamIDs = null;


 ExecutionHeader execHeader = new ExecutionHeader();
 rs.ExecutionHeaderValue = execHeader;
 rs.LoadReport(reportPath, historyID);

// Set DataSource Credentials
 // the user name is your userid and the password is the id of the organization
 // get more info here
 // http://ronaldlemmen.blogspot.com/2009/01/log-in-name-and-password-required-by.html
 DataSourceCredentials datasetCredential = new DataSourceCredentials();
 datasetCredential.DataSourceName = "DataSource1";
 datasetCredential.UserName = "F45D4E20-44DE-E011-8A08-005056860004";
 datasetCredential.Password = "D0E60C19-44DE-E011-8A08-005056860004";

// Set Report Parameter values
 ParameterValue[] myPVArray = new ParameterValue[1];
 ParameterValue myPV = new ParameterValue();
 myPV.Name = "ReportParameter1";
 myPV.Value = "True";
 myPVArray[0] = myPV;

rs.SetExecutionParameters(myPVArray, "en-us");


 DataSourceCredentials[] credentials = new DataSourceCredentials[1];
 credentials[0] = datasetCredential;
 rs.SetExecutionCredentials(credentials);

result = rs.Render(format, null, out extension, out encoding, out mimeType, out warnings, out streamIDs);

Response.Clear();
 MemoryStream ms = new MemoryStream(result);
 Response.ContentType = "application/pdf";
 Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
 Response.Buffer = true;
 ms.WriteTo(Response.OutputStream);
 Response.End();
 

Bye.


Filed under: CRM 2011, SqlServer Reporting Services Tagged: CRM 2011, CRM SQL Reporting Services, Reporting Services

This was originally posted here.

Comments

*This post is locked for comments