locked
Attaching a document to Annotations using javascript RRS feed

  • Question


  • Hello All,

    I am trying to upload a word document to Annotation (Notes) entity as an attachment using javascript. The note gets created with an attachment, but the attachment created is titled as "Untitled.txt" instead of the desired word document.

    What can I do to make sure the attachment is a word document instead of .txt? I want this to work for any document type (.docx, .pdf,.pptx).

    Here's my code so far.

    function CreateAnnotation()
            {
                var fileList = document.getElementById("myFile");
                var reader = new FileReader();
                
                reader.readAsDataURL(fileList.files[0]);
    
                var req = new XMLHttpRequest();
                req.open("POST", encodeURI("https://abcdev.api.crm.dynamics.com/api/data/v8.0/annotations"), true);
                req.setRequestHeader("Accept", "application/json");
                req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
                req.setRequestHeader("OData-MaxVersion", "4.0");
                req.setRequestHeader("OData-Version", "4.0");
                req.onreadystatechange = function () {
                    if (this.readyState == 4 /* complete */) {
                                            req.onreadystatechange = null;
                        if (this.status == 204) {
                            var notesUri = this.getResponseHeader("OData-EntityId");
                       }
                        else {
                            var error = JSON.parse(this.response).error;
                            alert(error.message);
                        }
                    }
                };
                req.send(JSON.stringify({
                    subject: "Sample note",
                    "objectid_new_crmdocument@odata.bind": "/new_crmdocuments(D8260C9B-4BCC-E511-80E3-C4346BACF5C0)",
                    documentbody: extractBase64(reader.result),
                    mimetype: "text/plain"
                }));
    
               
    
            }
    
          
    
            function extractBase64(dataURI)
            {
                
                var base64 = dataURI.split(',')[1];
                return base64;
    }

    Thanks,

    Swaroop

    Friday, February 19, 2016 10:04 PM

All replies

  • Hello,

    Try to use something similar to following:

                req.send(JSON.stringify({
                    subject: "Sample note",
                    filename: "WordDocument.docx",
                    "objectid_new_crmdocument@odata.bind": "/new_crmdocuments(D8260C9B-4BCC-E511-80E3-C4346BACF5C0)",
                    documentbody: extractBase64(reader.result),
                    mimetype: "text/plain"
                }));


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Monday, February 22, 2016 11:28 AM
    Moderator
  • Hi Andrii,

    Cool, it did work !

    Trying to understand why setting up a filename to <filename>.docx made it run? Even, should I be using the mimetype as "text/plain" for all types of file (.docx,.pptx,.jpeg)?

    Thanks,

    Swaroop

    Wednesday, February 24, 2016 9:54 PM