Saturday, November 5, 2016

Download Attachments in MSCRM using Javascript(HTML)



Recently we got a requirement to download the attachment files which are uploaded to a related records as Notes.
So, we created an HTML page which opens as a dialogue on click of a button. We need to retrieve the related annotations in HTML page or first retrieve them in button click javascript then pass them as a dialogue arguments. Then dynamically create an hyper links for each attachments in HTML, so that whenever user clicks that link, it should download the respective attachment file. Below is the code..!


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <title></title>
    <script src="../../ClientGlobalContext.js.aspx" type="text/javascript"></script>
    <script src="../scripts/jquery.js" type="text/javascript"></script>
    <script src="../scripts/json2.js" type="text/javascript"></script>
    <script src="../scripts/XrmServiceToolkit.js" type="text/javascript"></script>
    
    <script type="text/javascript">
        $(document).ready(function () {

            // Retrieve notes (OR) pass notes details from the parent window as a Dialogue Arguments
            var notes = retrieveNotesFromCurrentRecord();
            //var notes = window.getDialogArguments();

                for (var j = 0; j < notes.length; j++) {
                    $('#tbl_files tr:last ').after("<tr><td class='d0'>"
                        + (j + 1) + "</td><td class='d1'></td><td class='d2'>"
                        + notes[j].CreatedOn + "</td></tr>");

                    var fileLink = $("<a id='" + notes[j].AnnotationId + "' href='#'>"
                        + notes[j].FileName + "</a>");
                    fileLink.click(function () { download(this.id); });
                    $('#tbl_files tr:last .d1').append(fileLink);
                }
        });

        function retrieveNotesFromCurrentRecord() {

            // To-do: Logic to retrieve notes from the current record
        }

        function download(annotId) {

            var urlbase = window.parent.Xrm.Page.context.getClientUrl();
            var URL = urlbase + '/userdefined/edit.aspx?etc=5&id={' + annotId + '}';
            var docUrl;

            $.get(URL, function (data) {
                data = $.parseHTML(data);
                var securityTokenElement = $(data).find("[WRPCTokenUrl]");
                if (securityTokenElement) {
                    var securityTokenUrl = securityTokenElement.attr("WRPCTokenUrl");

                    if (securityTokenUrl) {
                        docUrl = urlbase + "/Activities/Attachment/download.aspx?AttachmentType=5&AttachmentId={"
                            + annotId + "}&IsNotesTabAttachment=undefined" + securityTokenUrl;
                    }
                }

                var element = document.createElement('a');
                element.setAttribute('href', docUrl);
                element.style.display = 'none';
                document.body.appendChild(element);
                element.click();
                document.body.removeChild(element);
            });
        }
    </script>
    
    <header>
        <div class="title" id="divTitle">Attchment Files</div>
        <div class="description" id="divDescription">Please click on the link to download the attachment.</div>
    </header>
    <div class="main">
        <div>
            <table id="tbl_files">
                <th>Sl. No</th>
                <th>Uploaded File</th>
                <th>Uploaded On</th>
            </table>
        </div>
    </div>
</body>
</html>

2 comments:

  1. Very interesting entry! :) I like to learn such new things. Now all the time I am looking for some cool solutions for my new company. This is a medical company. I found interesting solutions by Pro4People. I will be contacting them next week :) I hope that something nice will come out of it :)

    ReplyDelete
  2. Technical Treatment: Attachments In Mscrm Using Javascript(Html) >>>>> Download Now

    >>>>> Download Full

    Technical Treatment: Attachments In Mscrm Using Javascript(Html) >>>>> Download LINK

    >>>>> Download Now

    Technical Treatment: Attachments In Mscrm Using Javascript(Html) >>>>> Download Full

    >>>>> Download LINK TF

    ReplyDelete