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

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.


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

5 thoughts on “Sample Code to use ReportExecution2005.asmx to generate PDF in CRM 2011.”

  1. Hi,
    Thanks! This works very well.
    However, how do I pass a guid as parameter to the report, e.g. an accountid if I want to execute the report for a specific Account only?

    Like

  2. Hi, do you have any idea why it show System.Web.Services.Protocols.SoapException: The data source ‘MSCRM_FetchDataSource’ cannot be found. error when running the code above? FYI, I am able to generate the report in the Report Manager but running the code above failed. I tried published to external user but no luck.

    Like

  3. Microsoft SQL Server Reporting Service (SSRS) is an enterprise reporting server corporations use to develop, manage and distribute reports to the business. Reporting Services Web Service is the web-based capability, which uses Extensible Markup Language (XML), a standard for porting documents, and Simple Object Access Protocol (SOAP), a web service or protocol to exchange information on the web. SOAP is a web alternative to Hypertext Transfer Protocol (HTTP), a long-term standard used on the web. To access Reporting Services Web Services you will need to create an application in Microsoft Visual Studio.

    Like

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s