0
votes

I have an Entity in Dynamics CRM 2011 where license files are stored in base64-encoded format.

Up until now, the licenses has been delivered by email to the recipient (creating an e-mail activity and adding the file as attachment). I now also want to add the possibility to download the file directly from within Dynamics CRM.

Is there any way for a CRM 2011 Plugin to trigger a download of the file (base64-encoded string) to the client web browser?

I.e. I want the PostLicenseUpdate class / ExecutePostLicenseUpdate function to start/trigger a download of the file.

A very similar problem was solved by creating a separate .aspx on the webserver, however I'd prefer using a built in function i CRM.

2

2 Answers

1
votes

Here's my solution to the problem.

Except for jQuery, grab FileSaver.js, Blob.js and base64-binary.js.

Create your own download.js file as below, and add the addRequestDownloadLink-function to the OnLoad event of the form.

As the original license data is binary, you'll need to use Base64-binary.js instead of i.e. atob().

function addRequestDownloadLink() {
    $('#my_download').append('<div id="my_download_downloadcontainer"><a href="#" id="my_download_requestdownload">Request download of license file</a></div>');
}

$('body').on('click', '#my_download_downloadlink', function() {
    var str_decoded = Base64Binary.decode($(this).attr('data-license'));
    var blob = new Blob([str_decoded], {type: "license/binary"});
    saveAs(blob, $(this).attr('data-filename'));
});

$('body').on('click', '#my_download_requestdownload', function() {

    var guid = Xrm.Page.data.entity.getId();
    guid = guid.replace("{", ""); guid = guid.replace("}", "");

    var filename = Xrm.Page.getAttribute("my_name").getValue() + ".dat";
    var organization = Xrm.Page.context.getOrgUniqueName();
    var entity = "my_license";
    var select = "?$select=my_DataSigned"

    var oDataSelect = "/" + organization + "/XRMServices/2011/OrganizationData.svc/" + entity + "Set(guid'" + guid + "')" + select;

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: oDataSelect,
        beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data.d.my_DataSigned != null) {
                $('#my_download_downloadcontainer').html('<a href="#" id="my_download_downloadlink" data-license="' + data.d.my_DataSigned + '" data-filename="' + filename + '">Download license file!</span>');
            }
            else {
                alert("No license found!");
            }
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
        }
    });

});

Tadaaa! Hope this will help someone with the same issue.

0
votes

I don't think its possible using Plugins. You can try JavaScript.

Create a Custom button on Ribbon, and edit to run a javascript function on button click. Retrieve the value using OData call and write into a file. Then trigger the download. Have a look at this.

Note: If you are going to use this approach please be careful because there won't be any ribbons in CRM 2013. So in future, you might have to tweak it.