Forked from pawelgradecki/createActivityAttachment.js
Created
September 29, 2021 19:41
-
-
Save noorsyyed/b39de4720aaf8437bc9b829ebc46c40c to your computer and use it in GitHub Desktop.
How to create an attachment using WebApi in Dynamics 365
This file contains hidden or 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["[email protected]"] = "/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