none
Reports direkt als PDF oder Word anzeigen lassen

    Frage

  • Hallo zusammen.

    Ich weiß, das ich diese Frage schon einmal gestellt hatte.

    Leider schaffe ich es nicht z.B. in der Entität Angebote, einen Button zu bauen, das dieser einen bestimmten Bericht ausführt und im Word öffnet.

    Was habe ich bisher gemacht.

    Navigation - Navigationslink neu - Webressource - Neues Webressource hinzugefügt (Typ Webseite (HTML)

    Folgender Code ist dort hinterlegt:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="<head">http://www.w3.org/1999/xhtml"><head>
        <title></title>

        <script type="text/javascript">
            var Xrm;
            if (window.opener) { Xrm = window.opener.Xrm; }
            else if (window.parent) { Xrm = window.parent.Xrm; }

            function getReportingSession() {
                var reportName = "Inlinequote"; //set this to the report you are trying to download
                var reportId = "c33be413-15b7-e211-9282-00505684165b"; //set this to the guid of the report you are trying to download

                var pth = Xrm.Page.context.getServerUrl() + "/CRMReports/rsviewer/reportviewer.aspx";
                var retrieveEntityReq = new XMLHttpRequest();
                retrieveEntityReq.open("POST", pth, false);
                retrieveEntityReq.setRequestHeader("Accept", "*/*");
                retrieveEntityReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                retrieveEntityReq.send("id=%7B" + reportId + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false");

                var x = retrieveEntityReq.responseText.indexOf("ReportSession=");
                var ret = new Array();
                ret[0] = retrieveEntityReq.responseText.substr(x + 14, retrieveEntityReq.responseText.indexOf("&", x) - x - 14); //the session id
                x = retrieveEntityReq.responseText.indexOf("ControlID=");
                ret[1] = retrieveEntityReq.responseText.substr(x + 10, retrieveEntityReq.responseText.indexOf("&", x) - x - 10); //the control id
                return ret;
            }

            function createEntity(ent, entName, upd) {
                var jsonEntity = JSON.stringify(ent);
                var createEntityReq = new XMLHttpRequest();
                var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";
                createEntityReq.open("POST", ODataPath + "/" + entName + "Set" + upd, false);
                createEntityReq.setRequestHeader("Accept", "application/json");
                createEntityReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                createEntityReq.send(jsonEntity);
                var newEntity = JSON.parse(createEntityReq.responseText).d;
                return newEntity;
            }

            function createAttachment() {
                var params = getReportingSession();
                var post = Object();
                post.DocumentBody = encodePdf(params);
                post.Subject = "File Attachment";
                post.FileName = "Report.pdf";
                post.MimeType = "application/pdf";
                post.ObjectId = Object();
                post.ObjectId.LogicalName = Xrm.Page.data.entity.getEntityName();
                post.ObjectId.Id = Xrm.Page.data.entity.getId();
                createEntity(post, "Annotation", "");
            }


            var StringMaker = function () {
                this.parts = [];
                this.length = 0;
                this.append = function (s) {
                    this.parts.push(s);
                    this.length += s.length;
                }
                this.prepend = function (s) {
                    this.parts.unshift(s);
                    this.length += s.length;
                }
                this.toString = function () {
                    return this.parts.join('');
                }
            }

            var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

            function encode64(input) {
                var output = new StringMaker();
                var chr1, chr2, chr3;
                var enc1, enc2, enc3, enc4;
                var i = 0;

                while (i < input.length) {
                    chr1 = input[i++];
                    chr2 = input[i++];
                    chr3 = input[i++];

                    enc1 = chr1 >> 2;
                    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                    enc4 = chr3 & 63;

                    if (isNaN(chr2)) {
                        enc3 = enc4 = 64;
                   } else if (isNaN(chr3)) {
                        enc4 = 64;
                    }

                    output.append(keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4));
                }

                return output.toString();
            }

            var bdy = new Array();
            var bdyLen = 0;

            function concat2Bdy(x) {
                bdy[bdyLen] = x;
                bdyLen++;
            }

            function encodePdf(params) {
                bdy = new Array();
                bdyLen = 0;
                var retrieveEntityReq = new XMLHttpRequest();

                var pth = Xrm.Page.context.getServerUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + params[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + params[1] + "&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF";
                retrieveEntityReq.open("GET", pth, false);
                retrieveEntityReq.setRequestHeader("Accept", "*/*");

                retrieveEntityReq.send();
                BinaryToArray(retrieveEntityReq.responseBody);
                return encode64(bdy);
            }
    </script>
    <script type="text/vbscript">
    Function BinaryToArray(Binary)
           Dim i
           ReDim byteArray(LenB(Binary))
           For i = 1 To LenB(Binary)
                  byteArray(i-1) = AscB(MidB(Binary, i, 1))
                  concat2Bdy(AscB(MidB(Binary, i, 1)))
           Next
           BinaryToArray = byteArray
    End Function
    </script>      
    <meta charset="utf-8"></head>
    <body>
    <input onclick="createAttachment();" type="button" value="Attach Report">

    </body></html>

    Nach Betätigen des neuen Navigationsbutton sehe ich den Attach Report Button. Leider passiert dabei nichts.

    Kann einer denn Fehler entdecken oder hat einen anderen besseren Vorschlag?


    Danke und Gruß 


    Wolle39


    • Bearbeitet Wolle39 Mittwoch, 10. September 2014 07:35
    Dienstag, 9. September 2014 13:54

Alle Antworten

  • Hallo,

    Kann es sein, dass dein Browser durch eine Sicherheitseinstellung irgendwelche Funktionsaufrufe verhindert/blockt?! Ich würde den Netzwerk-Traffic nach der Betätigung des Buttons mal untersuchen...

    Liebe Grüße,

    Andreas


    Andreas Buchinger
    Microsoft Dynamics Certified Technology Specialist

    Mittwoch, 10. September 2014 11:58
  • Hallo Andreas.

    Du siehst also auch keinen Fehler? Die Browsereinstellungen werde ich prüfen.

    Lg Wolfi


    Wolle39

    Mittwoch, 10. September 2014 12:34