3

I have the gotten a certain result from the Notes tab.

The link you see inside the iframe is the name of the file.

I have the DocumentBody from the annotation in some format that looks like base64.

How do I download it?

Save base64 string to the client as a file

Thanks, Fabio

Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96
  • if you post a little of HTML we could understand better – freedev Dec 13 '12 at 18:41
  • Possible dupe? http://stackoverflow.com/questions/4274456/how-to-create-a-text-file-on-a-client-machine-using-javascript-or-jquery – Greg Owens Dec 14 '12 at 07:57
  • @GregOwens, the answer to your question is no, it's not a dupe. Even if the solution would be the same. The keywords used are not the same. So someone looking for these keywords will never find the other question. Besides, the question is not the same (even if the answer would). Try contributing instead of just trying to look smart by looking for other similar questions. Dupe syndrome is the only problem with this site. – Fabio Milheiro Dec 14 '12 at 09:25
  • C# example: http://woodsworkblog.wordpress.com/2012/07/28/exporting-annotation-note-attachment/ – James Wood Dec 14 '12 at 09:40
  • @JamesWood, thanks for the link but I was looking for a JavaScript solution. – Fabio Milheiro Dec 14 '12 at 10:11
  • Calm down Fabio, no need for such an aggressive tone ;) I contribute plenty as my profile attests. By finding a potential dupe I am potentially answering your question. In this case, you disagree that it's a dupe (Though you want to save a binary file to the client machine using Javascript. It sure *sounds* like the same thing). There's no need to get all sulky! – Greg Owens Dec 14 '12 at 10:45
  • Sorry if I seemed too aggressive. Didn't mean that but this is not a duplicate. – Fabio Milheiro Dec 14 '12 at 11:01
  • @FabioMilheiro That's why I didn't add as an answer, related information might come in handy. – James Wood Dec 14 '12 at 11:36

2 Answers2

1

Perform a JQuery request to a URL like this

Xrm.Page.context.getServerUrl() + "XRMServices/2011/OrganizationData.svc/ActivityMimeAttachmentSet(guid'abc...')?$select=Body"

By specifying the select you will request only what you want.

Assign the result to a variable and prepend

data:application/pdf;base64,



From there you could display it inline as an HTML object or try to open it as a new window with

window.location or window.open or document.location.href
Bvrce
  • 2,170
  • 2
  • 27
  • 45
  • Is it ActivityMimeAttachmentSet where the pdf is located? DO you have the GUID of the PDF? You can use Document.body.all.tags('a') and inspect the href attribute to find where the link is pointing to. – Bvrce Dec 14 '12 at 05:00
  • I am fairly certain that he means to **prepend** 'data:application/pdf;base64,' See the trailing comma? – DigitalDesignDj Dec 14 '12 at 05:17
  • @Bvrce, I got another solution, but I will accept your solution (which I had already tried) if you can show it is possible to do that. Please check my answer to see the problem I faced. Thanks. +1 – Fabio Milheiro Dec 14 '12 at 10:09
0

I had already the base64 documentbody string extracted like this:

function getSla() {
    // Define SOAP message
    var objectId;
    if (typeof crmForm === "undefined") {
        objectId = parent.crmForm.ObjectId;
    }
    else {
        objectId = crmForm.ObjectId;
    }

    var xml =
        [
        "<?xml version='1.0' encoding='utf-8'?>",
        "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" ",
        "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ",
        "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">",
        GenerateAuthenticationHeader(),
        "<soap:Body>",
        "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>",
        "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' ",
        "xsi:type='q1:QueryExpression'>",
        "<q1:EntityName>annotation</q1:EntityName>",
        "<q1:ColumnSet xsi:type='q1:AllColumns' />",
        "<q1:Distinct>false</q1:Distinct><q1:Criteria><q1:FilterOperator>And</q1:FilterOperator>",
        "<q1:Conditions><q1:Condition><q1:AttributeName>objectid</q1:AttributeName><q1:Operator>Equal</q1:Operator>",
        "<q1:Values><q1:Value xsi:type=\"xsd:string\">",
        objectId,
        "</q1:Value></q1:Values></q1:Condition></q1:Conditions></q1:Criteria>",
        "</query>",
        "</RetrieveMultiple>",
        "</soap:Body>",
        "</soap:Envelope>"
        ].join("");
    var resultXml = executeSoapRequest("RetrieveMultiple", xml);

    var result = filter(resultXml.getElementsByTagName("q1:filename"), function (element) {
        return /master.*sla/i.test(element.text);
    });

    if (result.length == 0) {
        return null;
    }
    else {
        return result[0].parentNode;
    }
}

function getSlaDocumentBody(sla) {
    return sla.getElementsByTagName("q1:documentbody")[0].text;
}

window.open("data:application/pdf;base64," + getSlaDocumentBody(sla));

It opened a new window with the string data:application/pdf.......... in the address bar but did nothing. I would prefer that solution indeed.

Ended up using srasmussen solution in here: http://social.microsoft.com/Forums/en/crm/thread/05134277-dd76-4fbb-8f6e-89b1a2a45af1.

var URL = serverUrl + "/userdefined/edit.aspx?etc=5&id=" + slaId;

$.get(URL, function (data) {
    var WRPCTokenElement = $(data).find("[WRPCTokenUrl]");
    if (WRPCTokenElement) {
        var WRPCTokenUrl = WRPCTokenElement.attr("WRPCTokenUrl");
        if (WRPCTokenUrl) {
            URL = "/Activities/Attachment/download.aspx?AttachmentType=5&AttachmentId=" + slaId + "&IsNotesTabAttachment=undefined" + WRPCTokenUrl;
            window.open(URL);
        }
    }

    return false;
});
jcjr
  • 1,503
  • 24
  • 40
Fabio Milheiro
  • 8,100
  • 17
  • 57
  • 96
  • I am busy working on something similar to preserve inline images in email activity responses. I will post when I have it properly solved. I have the Base64 png and can display it. Do you have the Base64 pdf text in a variable? – Bvrce Dec 14 '12 at 14:45
  • Yes @Bvrce, I have it. I get it from q1:documentbody as you can see in my answer. Thanks! Looking forward to your insight. – Fabio Milheiro Dec 15 '12 at 00:24