locked
Display email attachment images preview on Email form RRS feed

  • Question

  • Have a requirement to display email attachment images preview on Email form, I am using JavaScript Code in HTML Webresource, have attached HTML web resource on Email form.

    When I try to retrieve attachment body from activitymimeattachment entity, it is only returning 2000 characters because of single line text,

    If I directly decode my image and see, it is having more than 2000 characters.

    Problem is when I try to decode which I got from activitymimeattachment entity body, image is not displaying properly.

    Please let me know if there is any work around for this. Thanks

    Here is my javaScript code:

    <html>
    <head>
        <title>Email Attachment Preview</title>
        <script src="/WebResources/ava_/scripts/av_jquery_1.11.3.min.js" type="text/javascript"></script>
        <script src="/WebResources/ClientGlobalContext.js.aspx" type="text/javascript"></script>
        <script src="/WebResources/scripts/av_json2.js" type="text/javascript"></script>
        <script src="/WebResources/scripts/av_XrmServiceToolkit.js" type="text/javascript"></script>
        <script language="javascript" type="text/javascript">
            function previewEmailAttachments() {           
                //Get Email Activity RecordId
                var activityId = window.parent.Xrm.Page.data.entity.getId();
                if (activityId == null) {
                    return;
                }
                //Get Email Activity Attachments
                var emailAttachments = getEmailAttachments(activityId);
    
                if (emailAttachments != null && emailAttachments.length > 0) {
                    for (var i = 0; i < emailAttachments.length; i++) {
                        var mimetype = emailAttachments[i].attributes.mimetype.value;
                        var filename = emailAttachments[i].attributes.filename.value;
                        var body = emailAttachments[i].attributes.body.value;
                        var filesize = emailAttachments[i].attributes.filesize.value;
                        var imagefileContents = "data:" + mimetype + ";base64," + body;
                        var image = document.createElement('img');
                        image.setAttribute('style', 'width:auto;height:auto;');
                        image.setAttribute('src', imagefileContents);
                        var imageDiv = document.getElementById('imageDiv');
                        imageDiv.appendChild(image);
                    }
                }
            }
    
            //Get Email Activity Attachments
            function getEmailAttachments(emailactivityId) {
                var fetchXML = "<fetch distinct='true' no-lock='true' >";
                fetchXML += " <entity name='activitymimeattachment' >";
                fetchXML += "<attribute name='mimetype' alias='mimetype'  />";
                fetchXML += " <attribute name='filename' alias='filename' />";
                fetchXML += " <attribute name='body' alias='body'  />";
                fetchXML += " <attribute name='filesize' alias='filesize'  />";
                fetchXML += "<filter type='and' >";
                fetchXML += "<condition attribute='activityid' operator='eq' value='" + emailactivityId + "' />";
                fetchXML += " </filter>";
                fetchXML += " </entity>";
                fetchXML += "</fetch>";
    
                var emailAttachments = XrmServiceToolkit.Soap.Fetch(fetchXML);
    
                return emailAttachments;
            }
    
        </script>
    </head>
    <body onload="previewEmailAttachments();">
        <div id="imageDiv" style="width:auto;height:auto;">
        </div>
    </body>
    </html>



    • Edited by VSomireddy Monday, October 12, 2015 9:03 AM code update
    Monday, October 12, 2015 9:02 AM

Answers

  • we should use JQuery instead of XrmServiceToolKit to get the attachments data. XrmServiceToolKit does not return complete information.

    with JQuery it is working fine.

    here is JQuery code to get attachments data.

     //Get Email Activity Attachments
            function getEmailAttachments(activityId) {
                oDataURL = window.parent.Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ActivityMimeAttachmentSet?$select=Body,FileName,FileSize,MimeType&$filter=ActivityId/Id eq guid'" + activityId + "'";
                var emailAttachments = null;
                $.ajax({
                    type: "GET",
                    async: false,
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    url: oDataURL,
                    beforeSend: function (XMLHttpRequest) {
                        XMLHttpRequest.setRequestHeader("Accept", "application/json");
                    },
                    success: function (data, textStatus, XMLHttpRequest) {
                        emailAttachments = data.d.results;
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus + " " + errorThrown);
                    }
                });
    
                return emailAttachments;
            }

    • Marked as answer by VSomireddy Tuesday, October 13, 2015 6:58 AM
    Tuesday, October 13, 2015 6:58 AM

All replies

  • if you use following URL , you can see actual encoded text, compare this with CRM, you will see the difference, in CRM it allows only 2000 characters in body field.

    http://www.opinionatedgeek.com/DotNet/Tools/Base64Encode/

    • Marked as answer by VSomireddy Tuesday, October 13, 2015 6:56 AM
    • Unmarked as answer by VSomireddy Tuesday, October 13, 2015 6:56 AM
    Monday, October 12, 2015 10:49 AM
  • we should use JQuery instead of XrmServiceToolKit to get the attachments data. XrmServiceToolKit does not return complete information.

    with JQuery it is working fine.

    here is JQuery code to get attachments data.

     //Get Email Activity Attachments
            function getEmailAttachments(activityId) {
                oDataURL = window.parent.Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/ActivityMimeAttachmentSet?$select=Body,FileName,FileSize,MimeType&$filter=ActivityId/Id eq guid'" + activityId + "'";
                var emailAttachments = null;
                $.ajax({
                    type: "GET",
                    async: false,
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    url: oDataURL,
                    beforeSend: function (XMLHttpRequest) {
                        XMLHttpRequest.setRequestHeader("Accept", "application/json");
                    },
                    success: function (data, textStatus, XMLHttpRequest) {
                        emailAttachments = data.d.results;
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus + " " + errorThrown);
                    }
                });
    
                return emailAttachments;
            }

    • Marked as answer by VSomireddy Tuesday, October 13, 2015 6:58 AM
    Tuesday, October 13, 2015 6:58 AM