Trigger SSRS report from ribbon in CRM

Hello everyone,

In this article I will talk about how to trigger SSRS report from ribbon in CRM.

If you have worked with reports in CRM you may have noticed that you can reach your reports like below in a CRM Form:

 

crm run report button

 

 

But what if you are working with SSRS reports?

If you have not encountered such a situation you might not know but when you create a SSRS report and import it to CRM you cannot see it under Launch Report button like above. So, here we go!

There is a workaround for this situation.

If you have done with your SSRS report and already imported it from the Reports section on CRM you need to have a javascript web resource like below. I already explained the command lines as comments so I am not going to explain it again 🙂

// The function to be called from the Ribbon Button
function OpenReportPage() {

    try {

      	// report name: The name of the *.rdl file that you have already created in SSRS
        var rdlName = "myReport.rdl"; 

		// ReportId : This is the value of ReportId that you created from the Reports section in CRM, You can find this value via a writing a simply query to your database
      	var reportGuid = "5EE2338C-DCB8-E411-80D0-005056AA9999"; 

      	// Entity's Guid is being taken from the Form here
        var entityGuid = Xrm.Page.data.entity.getId(); 

        // The entity type code on which your Report will work (a numeric value)
      	var entityType = GetObjectTypeCode("contact"); // or another entity

        var link = GetServerUrl() + "/crmreports/viewer/viewer.aspx?action=run&context=records&helpID=" + rdlName + "&id={" + reportGuid + "}&records=" + entityGuid + "&recordstype=" + entityType;

      	// This methods opens the link above in a new 1024x768 sized window (Microsoft prefers to use this method while working with CRM instead of using classic window.open method
        openStdDlg(link, null, 1024, 768, true, false, null); 

    } catch (ex) {

        throw ex;

    }

}

// Helper functions

function GetObjectTypeCode(entityName) {

    try {

        var lookupService = new RemoteCommand("LookupService", "RetrieveTypeCode");

        lookupService.SetParameter("entityName", entityName);

        var result = lookupService.Execute();

        if (result.Success && typeof result.ReturnValue == "number") {

            return result.ReturnValue;

        } else {

            return null;

        }

    } catch (ex) {

        throw ex;

    }

}

function GetServerUrl() {

    var serverUrl = Xrm.Page.context.getClientUrl();

    return serverUrl;

}

function GetOrganizationName() {

    var orgName = Xrm.Page.context.getOrgUniqueName();

    return orgName;

}

Note: Do not forget to set the type of web resource as Script (JScript) when you create it.

Now it is time to add a button to ribbon and define a button command using Ribbon Workbench for Dynamics CRM 2013/2015. If you do not know how to add a button to your Ribbon go to this link and follow the explanations under Adding a button to the Ribbon and Defining a Button Command headers. At step 5 under Defining a Button Command header we refer to a javascript file from the Actions as shown below:

 

crm ribbon command 1

 

When you are in Actions form you need to add an Action to trigger our Javascript function. Here are the steps how you are going to do it:

1) Click Add button

2) Select you javascript webresource that you have already added to your solution

3) Type the name of the Function to be triggered to open your Custom SSRS report

 

crm ribbon command 2

 

Here it is. You might see the successful result hopefully. If you get an error after the new window is opened, it is probably because of javascript code. If you encounter an error it is highly recommended to review the entityname, report guid and report name of your rdl file.

If you have done so far you might have wondered how to pass current entity to your report. This way you can use queries of the current record of any entity to report. Here my Enabling pre-filtering in SQL Based SSRS reports in CRM article comes to the stage 🙂 You can follow the instructions that I explained there to pre-filter your SSRS report.

I hope you find this tutorial useful.

Happy Reportin’ 🙂

 

Leave a Reply

Your email address will not be published. Required fields are marked *