Today I will show how to "Send a PDF File as an attachment in Mail using java script in CRM 2015/2016. " To work around this we need to follow some steps:

Step 1:
Create a record in "Email" Entity with some subject value.

Step 2: Create a record for activityParty in "ActivityParty" Entity.

Step 3: Add the attachement of pdf in base64 to ActivityMimeAttachment and create a record in "ActivityMimeAttachment" Entity.

Step 4: Open the Email form based on the newly created activityId.
  1. CreateEmail: function()
  2. {
  3. var email = new Object();
  4. email.Subject = "Testing the sending of mail through java script";
  5. SDK.REST.createRecord(email, "Email", EmailCallBack, function(error)
  6. {
  7. alert(error.message);
  8. });
  9. },
  10. When the Email record is created with sucessfull, Then we have to create activity party
  11. for that email.
  12. // Email Call Back function
  13. EmailCallBack: function(result)
  14. {
  15. email1 = result;
  16. var activityParty = new Object();
  17. // Set the "party" of the ActivityParty // EntityReference of an entity this activityparty relatated to.
  18. activityParty.PartyId = {
  19. Id: Xrm.Page.context.getUserId(), // id of the the current user which becomes the sender
  20. LogicalName: "systemuser"
  21. };
  22. // Set the "activity" of the ActivityParty
  23. // EntityReference.
  24. activityParty.ActivityId =
  25. {
  26. Id: result.ActivityId,
  27. LogicalName: "email"
  28. };
  29. // Set the participation type (what role the party has on the activity).
  30. activityParty.ParticipationTypeMask =
  31. {
  32. Value: 1
  33. }; // 1 mean Sender
  34. SDK.REST.createRecord(activityParty, "ActivityParty", ActivityPartyCallBack, function(error) {
  35. alert(error.message);
  36. });
  37. },
When activity Party is created we need to attached a pdf file as base64 to the mail. For more information on how to create a pdf file follow my blog.
  1. ActivityPartyCallBack: function(result2)
  2. {
  3. // Generate the pdf file to attached to the Email.
  4. var responseSession = getReportingSession();
  5. encodePdf(responseSession);
  6. },
  7. // create a Email record with the attachement and other parameters.
  8. CreateEmailAttachment: function(bdy)
  9. {
  10. //Email attachment parameters
  11. var activitymimeattachment = Object();
  12. activitymimeattachment.ObjectId = Object();
  13. activitymimeattachment.ObjectId.LogicalName = "email";
  14. activitymimeattachment.ObjectId.Id = email1.ActivityId;
  15. activitymimeattachment.ObjectTypeCode = "email",
  16. activitymimeattachment.Subject = "File Attachment";
  17. activitymimeattachment.Body = bdy;
  18. activitymimeattachment.FileName = "xyz.pdf";
  19. //Attachment call
  20. activitymimeattachment.MimeType = "application/pdf";
  21. SDK.REST.createRecord(activitymimeattachment, "ActivityMimeAttachment", ActivityMimeAttachmentCallBack, function(error)
  22. {
  23. alert(error.message);
  24. });
  25. },
  26. ActivityMimeAttachmentCallBack: function(result)
  27. {
  28. var options =
  29. {
  30. openInNewWindow: true
  31. };
  32. Xrm.Utility.openEntityForm("email", email1.ActivityId, null, options);
  33. },
  34. //Encode the binary output pdf file to create an attachement
  35. encodePdf: function(responseSession)
  36. {
  37. var retrieveEntityReq = new XMLHttpRequest();
  38. var pth = Xrm.Page.context.getClientUrl() + "/Reserved.ReportViewerWebControl.axd?ReportSession=" + responseSession[0] + "&Culture=1033&CultureOverrides=True&UICulture=1033&UICultureOverrides=True&ReportStack=1&ControlID=" + responseSession[1] + "&OpType=Export&FileName=Public&ContentDisposition=OnlyHtmlInline&Format=PDF";
  39. retrieveEntityReq.open("GET", pth, true);
  40. retrieveEntityReq.setRequestHeader("Accept", "*/*");
  41. retrieveEntityReq.responseType = "arraybuffer";
  42. retrieveEntityReq.onreadystatechange = function()
  43. {
  44. if (retrieveEntityReq.readyState == 4 && retrieveEntityReq.status == 200) {
  45. var binary = "";
  46. var bytes = new Uint8Array(this.response);
  47. for (var i = 0; i < bytes.byteLength; i++)
  48. {
  49. binary += String.fromCharCode(bytes[i]);
  50. }
  51. var bdy = btoa(binary);
  52. CreateEmailAttachment(bdy);
  53. }
  54. };
  55. retrieveEntityReq.send();
  56. },
  57. getReportingSession: function()
  58. {
  59. var selectedIds = Xrm.Page.data.entity.getId();
  60. var reportName = "abc.rdl";
  61. var reportGuid = // Report GUID - Replace with your report GUID
  62. var pth = Xrm.Page.context.getClientUrl() + "/CRMReports/rsviewer/QuirksReportViewer.aspx";
  63. var retrieveEntityReq = new XMLHttpRequest();
  64. retrieveEntityReq.open("POST", pth, false);
  65. retrieveEntityReq.setRequestHeader("Accept", "*/*");
  66. retrieveEntityReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  67. retrieveEntityReq.send("id=%7B" + reportGuid + "%7D&uniquename=" + Xrm.Page.context.getOrgUniqueName() + "&iscustomreport=true&reportnameonsrs=&reportName=" + reportName + "&isScheduledReport=false&p:<parameters Name In ssrs>=" + selectedIds.toLowerCase().replace(/[^a-z0-9-]/g, ''));
  68. // p:<parameters Name In ssrs> :Is optional when you want to have parameter.
  69. var x = retrieveEntityReq.responseText.lastIndexOf("ReportSession=");
  70. var y = retrieveEntityReq.responseText.lastIndexOf("ControlID=");
  71. var ret = new Array();
  72. ret[0] = retrieveEntityReq.responseText.substr(x + 14, 24);
  73. ret[1] = retrieveEntityReq.responseText.substr(x + 10, 32);
  74. return ret;
  75. },
Hope this will help in your custom development. Leave your comment if you have any query.