Hi,
Follow the steps below to achieve this functionality.
1. Create a Global Action which accepts the input parameter as the RecordId.
See: http://www.powerobjects.com/2013/11/22/actions-in-dynamics-crm-2013/
2. Create a Javascript Web Resource with the code below.
function ExecuteGlobalAction(recordId, entityName) {
//Execute the created global action using Web API.
//get the current organization name
var serverURL = Xrm.Page.context.getClientUrl();
//query to send the request to the global Action
var query = "new_AttachFile";
//Pass the input parameters of action
var data = {
"RecordId": recordId,
"EntityName": entityName
};
//Create the HttpRequestObject to send WEB API Request
var req = new XMLHttpRequest();
//Post the WEB API Request
req.open("POST", serverURL + "/api/data/v8.2/" + query, 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) {
req.onreadystatechange = null;
if (this.status == 200) {
result = JSON.parse(this.response);
} else {
var error = JSON.parse(this.response).error;
alert(error.message);
}
}
};
//Execute request passing the input parameter of the action
req.send(window.JSON.stringify(data));
}
See: https://www.inogic.com/blog/2016/10/execute-the-global-action-using-web-api-in-dynamics-crm/
3. Open the Ribbon Workbench and create a new button and a new command.
See: https://ribbonworkbench.uservoice.com/knowledgebase/articles/71374-1-getting-started-with-the-ribbon-workbench
- If the button is created in Subgrid level, then in the command, Add Action as Javascript Action with Function Name as ExecuteGlobalAction and in the Parameters, add CRM Parameters as FirstSelectedItemId and SelectedEntityTypeName.
- If the button is created in Form level, then in the command, Add Action as Javascript Action with Function Name as ExecuteGlobalAction and in the Parameters, add CRM Parameter as FirstPrimaryItemId and PrimaryEntityTypeName.
4. Create a Plugin to choose a file using dialog, read the file and convert it to the base64 string. Create an annotation record with the base64string as documentbody field, filename field as the name of the file read, isdocument field as true, objectid field as the RecordId (passed from Javascript as input parameter of action), objecttypecode field as the entityName (passed from Javascript as input parameter of action).
See: https://stackoverflow.com/questions/25919387/c-sharp-converting-file-into-base64string-and-back-again
5. Register the Plugin Assembly using Plugin Registration Tool and register a new step as shown below.
See: https://www.greenbeacon.com/insights/trigger-plugin-from-ribbon-button-using-custom-actions-in-dynamics-crm-2013/
Hope this helps.