Calling CRM Reports from .Net Code

On many occasions we need to execute CRM reports from a .Net code component and save the results which are returned as byte array results. The mentioned below code snippet shows how that could be accomplished.

For calling a CRM report we need the following values

  1. CRM Report URL(reportServerURL) – http://{crmreportserver}/ReportServer/ReportService2005.asmx
  2. CRM Report Execution URL (reportExecutionURL) – http://{crmreportserver}/ReportServer/ReportExecution2005.asmx
  3. CRM Report Path – Path of the report which needs to be called.
  4. ReportingServerAdminCredentials – Domain name, Username and password of the user which is having report browsing right on the report being called.

Mentioned below is the code snippet for the same

  • When we call the reporting server we can specify the document type and the orientation¬†in which we want the results to be returned. For example in the below example, we are calling the reporting server and passing document type as word and orientation as landscape.
  • If there is a custom datasource that is declared in the report, we can also pass the corresponding login credentials for the database. In the below example, its shown how that could be achieved.

public byte[] ReadWordFormatFile(string dealID, string reportServerURL, string reportExecutionURL, string reportURL, string dbConnectionName, string username, string password, NetworkCredential reportingCredential)
{
string historyID = null;
string deviceInfo = “<DeviceInfo><OutputFormat>WORD</OutputFormat><Orientation>Landscape</Orientation><PageWidth>11in</PageWidth><PageHeight>8.5in</PageHeight><MarginTop>0.25in</MarginTop><MarginLeft>0.25in</MarginLeft><MarginRight>0.25in</MarginRight><MarginBottom>0.25in</MarginBottom></DeviceInfo>”;

string format = “Word”;
Byte[] results;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
string[] streamIDs = null;
RExecutionProxy.Warning[] warnings = null;

// Name of the report – Please note this is not the RDL file.
try
{
ReportingService2005 rs;
ReportExecutionService rsExec;
rs = new ReportingService2005();
rsExec = new ReportExecutionService();
rs.Credentials = reportingCredential;
rsExec.Credentials = reportingCredential;
rs.Url = reportServerURL;
rsExec.Url = reportExecutionURL;
RExecutionProxy.DataSourceCredentials cred = new RExecutionProxy.DataSourceCredentials();
cred.DataSourceName = dbConnectionName;
cred.UserName = username;
cred.Password = password;
RExecutionProxy.ExecutionInfo ei = rsExec.LoadReport(reportURL, historyID);
RExecutionProxy.DataSourceCredentials[] credentials = new RExecutionProxy.DataSourceCredentials[1];
credentials[0] = cred;
rsExec.SetExecutionCredentials(credentials);
rs.Url = reportServerURL;
rsExec.Url = reportExecutionURL;
RExecutionProxy.ParameterValue[] parameters = new RExecutionProxy.ParameterValue[1];
parameters[0] = new RExecutionProxy.ParameterValue();
parameters[0].Name = “DealID”;
parameters[0].Value = dealID;
rsExec.SetExecutionParameters(parameters, “en-us”);
results = rsExec.Render(format, deviceInfo,
out extension, out encoding,
out mimeType, out warnings, out streamIDs);

return results;
}
catch (Exception ex)
{
LogDispatcher.Logger.Log(ex);
return null;
}}

 

Advertisements

Leave a Reply

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s