{Dynamics CRM – SSRS} Execute custom SSRS report from Microsoft Dynamics CRM using custom code.

Recently we had the requirement in our project where the customer wanted to execute the Export To Excel feature for an editable grid that we implemented. The following in an overview of the requirement.

1. There is a custom jQuery Grid which shows a consolidated view to customer for a custom entity and its related child entities. The grid is an editable grid which was accomplished through a CRM webresource.

2. For that grid, the customer wanted an export to excel feature with the following functionality

  • Excel report in the same format as the grid is appearing
  • Excel report would give the user option to

                 1. Export selected rows
                 2. Export all records on the current page
                 3. Export All records.

To meet the above requirement we did the following.

1. Created a webresource in CRM with the same look and feel as OOB Export To Excel pop-up, with the options the user was looking for.

2. Created custom SSRS reports in the same format as the grid and deploy it to the report server

3. Set up a custom website (CRM 2013) or Web application under ISV folder (CRM 2011), call the website page with the user choices passed as the querystring and execute the SSRS report based using asp.net code in the page

The following is the code to render the ssrs report to the client in excel format. Add a reference to Microsoft.ReportViewer.WebForms dll in your code and include the namespace Microsoft.Reporting.WebForms.

// Parse the query strings. The querystrings would be basically your user choices
<Your logic goes here>

var reportViewer = new ReportViewer();

reportViewer.ProcessingMode = ProcessingMode.Remote;

// Set the username and password for connecting to reportserver
var domain = ConfigurationManager.AppSettings[“ServerDomain”].ToString();
var userName = ConfigurationManager.AppSettings[“ServerUsername”].ToString();
var password = ConfigurationManager.AppSettings[“ServerPassword”].ToString();
IReportServerCredentials irsc = new CustomReportCredentials(userName, password, domain);
reportViewer.ServerReport.ReportServerCredentials = irsc;

// set the reportserverurl. Typically it would be in the format of http://<servername>/ReportServer
reportViewer.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings[“ReportServerUrl”]);

// set the report path i.e the path where the report has been deployed in the report server. You do not need to specify .rdl extension here.
reportViewer.ServerReport.ReportPath = “/TestReportFolder/TestReport”;

//populate the parameter list if any paramter is required by the report from the querystring.
var paramList = new System.Collections.Generic.List<Microsoft.Reporting.WebForms.ReportParameter>();

//for example, here i am passing the loggedin userid and the report is expecting a parameter @UserId
paramList.Add(new ReportParameter(“UserId”, userid));


// declare variables
string mimeType, encoding, extension, deviceInfo;
string[] streamids;
Microsoft.Reporting.WebForms.Warning[] warnings;
string format = “Excel”;

deviceInfo =
“<DeviceInfo>” +
“<SimplePageHeaders>True</SimplePageHeaders>” +

byte[] bytes = reportViewer.ServerReport.Render(format, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);

// set the excel file name

var fileName = “ExcelFile.xls”;

Response.ContentType = “application/excel”;
Response.AddHeader(“Content-disposition”, “attachment;filename=” + fileName);

Response.OutputStream.Write(bytes, 0, bytes.Length);

and following is the code for the class CustomReportCredentials

internal class CustomReportCredentials : IReportServerCredentials
private string _UserName;
private string _PassWord;
private string _DomainName;

public CustomReportCredentials(string UserName, string PassWord, string DomainName)
_UserName = UserName;
_PassWord = PassWord;
_DomainName = DomainName;

public System.Security.Principal.WindowsIdentity ImpersonationUser
get { return null; }

public ICredentials NetworkCredentials
get { return new NetworkCredential(_UserName, _PassWord, _DomainName); }

public bool GetFormsCredentials(out Cookie authCookie, out string user,
out string password, out string authority)
authCookie = null;
user = password = authority = null;
return false;

And the user would get a pop-up from the browser to download the excel file.


Hope this helps!

About Debajit
I am a Dynamics CRM Most Valuable Professional (MVP) with 12 years of experience in Microsoft .NET Technologies and 9 years of dedicated experience in Microsoft Dynamics CRM. I have worked with companies like Microsoft, SanDisk, PwC, TMF Group and have extensive experience of implementing complex CRM solutions from both offshore and client side. Currently the face of XrmForYou.com with significant experience in delivering corporate training on Dynamics CRM and have already delivered multiple projects to client through XrmForYou.com Author of multiple tools on codeplex including the 'Role Based Views' and 'CRM-Sharepoint Metadata manager & Attachment Extractor' which are available for commercial use under XrmForYou.com For consulting/ training, drop me a note at info@xrmforyou.com or visit our website www.xrmforyou.com

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

%d bloggers like this: