Created
July 19, 2017 14:57
-
-
Save pawelgradecki/61150022552056a4c3ed8b5fd52675a7 to your computer and use it in GitHub Desktop.
How to create an attachment using WebApi in Dynamics 365
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment