web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Generate file on button click

(0) ShareShare
ReportReport
Posted on by 175

Hello,

I have a button on record, on click of that button it should fetch some data and download it in excel for the user.

I have a wcf service which is called on that button for fetching the data.

I need ideas on how to generate and download the file for end user.

Any help ?

Thanks.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Tomas Prokop Profile Picture
    590 on at

    Hi,

    you have two options:

    1) Do it all client side and use something like this: github.com/.../excel-builder.js

    2) Go server side. It is a way easier to consume WCF from .NET.

    • Create an Action and call your custom .NET plugin which will generate the output file and return it as Base64.
    • Call the action from the client side using process.js.
    • Use a mechanism like FileSaver.js to support larger files github.com/.../FileSaver.js

    It can be quick and easy for a skilled Dynamics dev but I understand you can have a hard time plugging this together if you don't work with it on a daily basis.

    If you were lost I could help you with the implementation.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
  • LBA Profile Picture
    175 on at

    I am calling wcf service from javascript but I am not sure what should be returned from the service whether returning stream is a good idea or json and then after creating file from it.

  • gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Hope you exposed wcf service as rest enabled and then using javascript make XmlHttpRequest   receive the result ,once you get the  result just loop through each record and add the data.in the workbook column.

  • LBA Profile Picture
    175 on at

    Hi,

    I have created excel in a memorysteam in wcf and returned it. I am now looking for a way to make it downloadable from javascript.

  • Suggested answer
    Tomas Prokop Profile Picture
    590 on at

    Look at FileSaver.js github.com/.../FileSaver.js

    var blob = new Blob([byte array fom web service], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
    
    FileSaver.saveAs(blob, "hello.xlsx");


  • LBA Profile Picture
    175 on at

    Hi Tomas,

    I have added FileSaver.js on the form and added these two lines. I am getting error saying 'saveAs' is undefined. It seems it unable to reference the file. Am I missing anything ?

  • Tomas Prokop Profile Picture
    590 on at

    What version of Dynamics 365 are you running? it is possible to manage JS dependencies in 9.0.
    chrome_5F00_2018_2D00_05_2D00_27_5F00_17_2D00_19_2D00_25.png

    What type of Web Resource is your script? JScript or HTML Webpage with button?

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    Return your response text  or file content in Base64 format in your WCF service like below in response object .

       Response.FileContent = Convert.ToBase64String(_fileContentText);
    Response.FileName ="Test.Xls";


    In the client side here is the sample code to consume function and download the file -

      var objRequest = {
                            Parameter1 : hdrType,
    Parameter2 : FileType. };


    jQuery.support.cors = !0, $.ajax({
        url: WCFServiceURL,
        data: JSON.stringify(objRequest),
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        cache: !1,
        async: true,
        success: function (ResponseObj) {
            if (ResponseObj != null && ResponseObj != undefined) {
                var fileCont = ResponseObj.FileContent;
                var fileName = ResponseObj.FileContent;       
                var samplebyte = base64ToArrayBuffer(fileCont);
                saveByteArray([samplebyte], fileName);
            }
        },
        error: function (error) {
            alert(error);
        }
    });
    
    
    function base64ToArrayBuffer(base64) {
        var binaryString = window.atob(base64);
        var binaryLen = binaryString.length;
        var bytes = new Uint8Array(binaryLen);
        for (var i = 0; i < binaryLen; i++) {
            var ascii = binaryString.charCodeAt(i);
            bytes[i] = ascii;
        }
        return bytes;
    }
    
    var saveByteArray = (function () {
        var a = document.createElement("a");
        document.body.appendChild(a);
        a.style = "display: none";
        return function (data, name) {
            var blob = new Blob(data, { type: "octet/stream" }),
                url = window.URL.createObjectURL(blob);
            window.navigator.msSaveOrOpenBlob(blob, name);
        };
    }());


    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans