Upload Multiple Files in Dynamics 365 CE


  1. Create a Script file and copy the below script.
  2. Change the highlighted entity name with the entity logical name to which files need to uploaded.
  3. I have used XMLHttpRequest to make it sysnchronous calls in order to refresh the Timeline after completing upload of all files. If Refresh is not needed use Xrm.WebApi API (WebApi.createRecord(“annotation”, entity).then(function success(result) { console.log(“Success”); }, function (error) { console.log(error.message); });)
//Pick File Feature

function pickFile(formContext) {

//Pick Files. Multiple files can be uploaded if allowMultipleFiles is true

Xrm.Device.pickFile({ allowMultipleFiles: true }).then(

function (result) {

var entityId = formContext.data.entity.getId();

if (result.length > 0) {

for (var i = 0; i < result.length; i++) {

var fileContent = result[i].fileContent;

var fileName = result[i].fileName;

var mimeType = result[i].mimeType;

UploadFilesToNotes(formContext, fileName, mimeType, fileContent, entityId);

}

formContext.getControl(“Timeline”).refresh();

}

},

function (error) {

alert(error.message);

});

}

 

//This function will create the Notes

function UploadFilesToNotes(formContext, fileName, mimeType, fileContent, entityId) {

entityId = entityId.replace(‘{‘, ”).replace(‘}’, ”);

//Build the Data

var entity = {};

entity.documentbody = fileContent;

entity.filename = fileName;

entity.mimetype = mimeType;

entity.notetext = “New Attachment”;

entity[“objectid_new_kdtest@odata.bind”] = “/new_kdtests(” + entityId + “)”;

entity.subject = “New File Uploaded”;

//Using XMLHttpRequest to make it sysnchronous calls in order to refresh the Timeline after completing upload of all files. If Refresh is not needed use Xrm.WebApi API

var req = new XMLHttpRequest();

req.open(“POST”, formContext.context.getClientUrl() + “/api/data/v9.1/annotations”, false);

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));

}

  1. Create a Command on the Entity using Ribbon Workbench
  1. Add New Command and Create Custom Java Script Action and add CRM Parameter as Primary Control (Form Context)
  1. Add Display Rule ā€“ to show the Upload Files only for existing Records.
  1. Publish the Ribbon Changes.

Note: This will work only for UCI.

Good Luck šŸ™‚

Advertisements

About Kishore Dhanekula

MS Dynamics CRM and .NET Architect
This entry was posted in CRM, Dynamics 365, MS Dynamics 365, MS Dynamics 365 CE and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s