Very short post today – just wanted to share with you a piece of code which you can use to create an attachment for any activity in Dynamics 365. This is different from normal attachments, because you are not doing this by adding Annotation, rather ActivityMimeAttachment object. It’s important to remember that when you are migrating data – moving only Annotations will not transfer all Email or Appointments attachments to the new system!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var activityId = "059C0532-906C-E711-9409-00155D018D00"; | |
var activityType = "appointment"; //or any other entity type | |
var entity = {}; | |
entity["objectid_activitypointer@odata.bind"] = "/activitypointers(" + activityId + ")"; | |
entity.body = "ZGZnZA=="; //your file encoded with Base64 | |
entity.filename = "test"; | |
entity.subject = "test"; | |
entity.objecttypecode = activityType; | |
var req = new XMLHttpRequest(); | |
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/activitymimeattachments", true); | |
req.setRequestHeader("OData-MaxVersion", "4.0"); | |
req.setRequestHeader("OData-Version", "4.0"); | |
req.setRequestHeader("Accept", "application/json"); | |
req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); | |
req.onreadystatechange = function() { | |
if (this.readyState === 4) { | |
req.onreadystatechange = null; | |
if (this.status === 204) { | |
var uri = this.getResponseHeader("OData-EntityId"); | |
var regExp = /\(([^)]+)\)/; | |
var matches = regExp.exec(uri); | |
var newEntityId = matches[1]; | |
} else { | |
Xrm.Utility.alertDialog(this.statusText); | |
} | |
} | |
}; | |
req.send(JSON.stringify(entity)); |