Thursday, May 22, 2008

Displaying a Report in an IFrame of a CRM Entity Form

Zahara Hirani published the following on his blog.
------------------------------
Recently Danny Varghese wrote a blog on displaying related entities in an IFrame of a CRM form. Let’s give it a little twist – Displaying a Report in an IFrame of a CRM Form.

For this example, we created a custom report that takes in the Account Id as a parameter and displays the campaign activities associated to that Account. Now let’s add the report to display on an account and show all campaign activities that have taken place with that account.

For this,

Open CRM and navigate to the Settings Module.
Now navigate to Customizations and select Customize Entities.
Select the Account entity for edit and navigate to the Forms and Views.
Select the Form for edit.
Add a Tab to the Account called ‘Campaign Activity’.
Add a section in that tab called ‘Campaign Activity Report’ and add an IFrame to the section.
For the URL of the IFrame, enter ‘about:blank’.
We will now add logic in the form OnLoad event that will create the report URL and append the Account Id for the report to use to display the campaign activities.

//Get Account Id and remove the {} from the start and end of the guid
var accountId = crmFormSubmit.crmFormSubmitId.value;
accountId = accountId.substring(1, accountId.length -1)

//Get Reporting URL
var url = 'http://localhost:5555/ReportServer?%2fCrowe+Chizek_MSCRM%
2fAccount+Campaign+Activity&rs:Command=Render&rs:Format=HTML4.0&
rc:Toolbar=false&rc:Parameters=false&AccountId=' + accountId;

document.getElementById('IFRAME_CampaignActivity').src = url;

In the above code you will notice, we had to remove the ‘{}’ from the account id the Form returns. The easiest way to get the report URL is to open the report using the report server.

For this,

Navigate to the reporting services report server (e.g. http://localhost/reportserver)
Now Navigate to the CRM Reports Folder. (e.g. Crowe_Chizek_MSCRM)
Now navigate to the custom report you created. (e.g. Account Campaign Report in our scenario).

Since we want the report in an IFrame, we do not want the report viewer toolbar and parameters to show. Hence the ‘rc:Toolbar=false&rc:Parameters=false”. Since we want the report to render in HTML 4.0, we can specify the format to render the report in by specifying ‘rs:Format=HTML4.0’. Last but not the least we add the Account Id to the querystring so that the report can pick up the Account Id from the querystring and use it to return only campaign activities associated to that account.

Now save and close the form and publish the entity.

Navigate to an account that has campaign activities and you should see the new tab and report render in the IFrame.

No comments: