Showing posts with label Crm_Reports. Show all posts
Showing posts with label Crm_Reports. Show all posts

Monday, 23 September 2013

Adding a custom button on report viewer window for SSRS reports

Add a clear button to SQL Server Reporting Services

If you ever had the need to add a clear / reset button to your standard SQL Server Reporting Services report viewer, here’s a way to do it. Normally when reports are displayed, they are piped through the ReportViewer.aspx page that comes with SSRS. This page hosts the Reporting Server host component, and adds text boxes, radio buttton etc. based on the number of parameters you have in your report. So here’s a way to add a clear button to SQL Server Reporting Services.
Something like this:
You can’t simply replace this file with your own custom page, because SSRS has HTTP handlers installed that prevents any other file to be rendered except the ReportViewer.aspx page.
So how to add a clear button to clear the text boxes? One way to do it is to modify the OOB ReportViewer.aspx page by injecting some javascript that does this for us. Initially I wanted to use jQuery, but again, the HTTP handler prohibits us from accessing the external .js file. Back to plain old Javascript it is.
Essentially, we just need to find the container that holds the View Report button, and add our custom button.
In the body tag, add a page onload event handler:
<body style="margin: 0px; overflow: auto" onload="addClearButton();">
and then add some javascript code:
<script type="text/javascript">
document.getElementsByClassName = function(cl) {
    var retnode = [];
    var myclass = new RegExp('\\b'+cl+'\\b');
    var elem = this.getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) {
        var classes = elem[i].className;
        if (myclass.test(classes)) retnode.push(elem[i]);
    return retnode;

function addClearButton(){
    var inputs = document.getElementsByClassName('SubmitButtonCell');

    // can't find the cell, return
    if (inputs.length<1)

    // create a button
    var clearButton = document.createElement("input");
    clearButton.type = "button";
    clearButton.value = "Clear"; = "btnClear"; = "100%";

    // add clear text boxes functionality to the onclick event
    clearButton.onclick = function (){
        var textBoxes = document.getElementsByTagName("input");
        for (var i=0;i<textBoxes.length;i++){
        if (textBoxes[i].getAttribute("type")=="text"){
          textBoxes[i].value ="";

    // find the relevant cells
    var tdSubmitButtonCell = inputs[0];

    // find the child table
    var table = tdSubmitButtonCell.childNodes[0];
    var lastRow = table.rows.length;
    var row = table.insertRow(lastRow);
    var cellLeft = row.insertCell(0);

    // add the clear button


The final result will look something like this:
The code can be found on at: Custom SQL Server Reporting Services ReportViewer.aspx page
posted in Blog by Magnus Johansson

Run a report from a ribbon button in mscrm 2011

To run a report from a ribbon button you need to create a js file with a function you'll be calling from your button.
You need 4 things:
  1. rdlName - rdl file name.
  2. reportGuid GUID of the report.
  3. entityGuid = Entity GUID wich you run report for.
  4. entityType = Entity Object Type Code.
Here is the example.
function printOutOnClick() {
    // This function generates a Print out
    var rdlName = "SomeReport.rdl";
    var reportGuid = "9A984A27-34E5-E011-B68F-005056AC478A";
    var entityGuid =;//Here I am getting Entity GUID it from it's form
    var entityType = "4214";
    var link = serverUrl + "/" + organizationName + "/crmreports/viewer/viewer.aspx?action=run&context=records&helpID=" + rdlName  + "&id={" + reportGuid + "}&records=" + entityGuid + "&recordstype=" + entityType;
    openStdDlg(link, null, 800, 600, true, false, null);
openStdDlg() is the wrapper around MS Dynamics CRM uses it itself, so do I.
To add it to a ribbon button you need to do like in this post How to start a Dialog from Application Ribbon (CRM 2011) except you need to call report instead a dialog.

Extracting the SSRS Report in PDF programmatically using Plugin or Wokflow in mscrm 2011

Extracting the Report in PDF programmatically.
Code snippet:
ReportGenerator rg = new ReportGenerator("http://crm/Reportserver/ReportExecution2005.asmx",
                new NetworkCredential("administrator", "Password", "contoso"));

            ParameterValue[] parameters = new ParameterValue[1];
            parameters[0] = new ParameterValue();
            parameters[0].Name = "P1";
            parameters[0].Value = string.Format("Select * From FilteredQuote Where QuoteId = '{0}'",

            byte[] reportresult = rg.Render("/contoso_mscrm/quote", FormatType.PDF, parameters);

            Entity attachment = new Entity("activitymimeattachment");
            attachment["objectid"] = Email.Get<EntityReference>(executionContext);
            attachment["objecttypecode"] = "email";
            attachment["filename"] =
            attachment["subject"] = "Quote.pdf";
            attachment["body"] = System.Convert.ToBase64String(reportresult);

Ref Links:

Saturday, 27 April 2013

CRM 2011 Embed Report in IFrame and Pass Parameters from the Form

In CRM you can insert SubGrid and Chart. However this functionality is limited in a way that it can only grab the related entities that have direct relationship to that entity. If we want to get the other related entities from the related entity, it's not possible using the OOTB Subgrid and Chart.

In my case I want to display a chart in an Engagement Plan entity. This Engagement Plan is associated with an Account (N:1). The Account has 1:N relationships with Production History and Contract History entities that have information about commodity and tonnes produced. I want that chart to be shown on the Engagement Plan form. The parameters for this chart will be the Account Id and the Season Id located on the form.

The Steps:
  1. Create a custom SSRS Report to display this chart, taking @CRM_AccountId and @CRM_SeasonId as parameters.
  2. Insert an IFRAME on the form and give dummy Url. Also uncheck 'Restrict Cross Site Scripting' to allow cross scripting between servers. 
  3. Create a javascript that is triggered in Form_OnLoad, Account_OnChange, and Season_OnChange:

    // JScript source code
    function Form_onload() {
    function loadChart() {
        var growerLookupItem = new Array();
        growerLookupItem = Xrm.Page.getAttribute("new_growerentityid").getValue();
        var seasonLookupItem = new Array();
        seasonLookupItem = Xrm.Page.getAttribute("new_seasonid").getValue();
        var serverUrl = Xrm.Page.context.getServerUrl();
        var url = serverUrl + "/crmreports/viewer/viewer.aspx?action=filter&helpID=CommodityTonnes.rdl&id=%7bB135D3F7-65C1-E011-A653-005056A10003%7d";
        SetReportUrl(url, "IFRAME_CommodityTonnes", growerLookupItem, seasonLookupItem);
    function SetReportUrl(reportUrl, iFrame, growerLookupItem, seasonLookupItem) {
        if (growerLookupItem == null || seasonLookupItem == null) {
        else {
            var growerId = growerLookupItem[0].id;
            var seasonId = seasonLookupItem[0].id;
            Xrm.Page.getControl(iFrame).setSrc(reportUrl + "&p:CRM_AccountId=" + growerId + "&p:CRM_SeasonId=" + seasonId);

The method for the form OnLoad is 'Form_onload'
The method for the field Onchange is 'loadChart'