【原创】CRM 2015/2016,SSRS 生成PDF文件,幷以附件的形式发送邮件
主要步骤如下:
- 生成一条邮件记录
- 生成一条ActivityParty记录
- 生成PDF文件,并以Base64添加到ActivityMimeAttachment 中去
- 打开发送邮件窗口,以便编辑及发送邮件
第一,生成邮件记录
CreateEmail: function () { var email = new Object(); email.Subject = "Quotation"; SDK.REST.createRecord(email, "Email", EmailCallBack, function (error) { alert(error.message); }); },
第二,当成功建好邮件记录后,我们来生成ActivityParty记录
// Email Call Back function EmailCallBack: function (result) { email1 = result; var activityParty = new Object(); // Set the "party" of the ActivityParty // EntityReference of an entity this activityparty relatated to. activityParty.PartyId = { Id: Xrm.Page.context.getUserId(), // id of the the current user which becomes the sender LogicalName: "systemuser" }; // Set the "activity" of the ActivityParty // EntityReference. activityParty.ActivityId = { Id: result.ActivityId, LogicalName: "email" }; // Set the participation type (what role the party has on the activity). activityParty.ParticipationTypeMask = { Value: 1 }; // 1 mean Sender SDK.REST.createRecord(activityParty, "ActivityParty", ActivityPartyCallBack, function (error) { alert(error.message); }); },
第三,当成功建好ActivityParty记录后,调用SSRS生成PDF形式的报表,并以附件形式附加到邮件中去
ActivityPartyCallBack: function (result2) { // Generate the pdf file to attached to the Email. var responseSession = getReportingSession(); encodePdf(responseSession); },
// create a Email record with the attachement and other parameters. CreateEmailAttachment: function (bdy) { //Email attachment parameters var activitymimeattachment = Object(); activitymimeattachment.ObjectId = Object(); activitymimeattachment.ObjectId.LogicalName = "email"; activitymimeattachment.ObjectId.Id = email1.ActivityId; activitymimeattachment.ObjectTypeCode = "email", activitymimeattachment.Subject = "File Attachment"; activitymimeattachment.Body = bdy; activitymimeattachment.FileName = "Quotation.pdf"; //Attachment call activitymimeattachment.MimeType = "application/pdf"; SDK.REST.createRecord(activitymimeattachment, "ActivityMimeAttachment",ActivityMimeAttachmentCallBack, function (error) { alert(error.message); }); },
第四,打开邮件编辑窗口,以便编辑及发送邮件。
ActivityMimeAttachmentCallBack: function (result) { var options = { openInNewWindow: true }; Xrm.Utility.openEntityForm("email", email1.ActivityId, null, options); },
以下是SSRS生成PDF的代码。
//Encode the binary output pdf file to create an attachement encodePdf: function (responseSession) { var retrieveEntityReq = new XMLHttpRequest(); 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"; retrieveEntityReq.open("GET", pth, true); retrieveEntityReq.setRequestHeader("Accept", "*/*"); retrieveEntityReq.responseType = "arraybuffer"; retrieveEntityReq.onreadystatechange = function () { if (retrieveEntityReq.readyState == 4 && retrieveEntityReq.status == 200) { var binary = ""; var bytes = new Uint8Array(this.response); for (var i = 0; i < bytes.byteLength; i++) { binary += String.fromCharCode(bytes[i]); } var bdy = btoa(binary); CreateEmailAttachment(bdy); } }; retrieveEntityReq.send(); },
getReportingSession: function () { var selectedIds = Xrm.Page.data.entity.getId(); var reportName = "quotation.rdl"; var reportGuid = // Report GUID - Replace with your report GUID var pth = Xrm.Page.context.getClientUrl() + "/CRMReports/rsviewer/QuirksReportViewer.aspx"; var retrieveEntityReq = new XMLHttpRequest(); retrieveEntityReq.open("POST", pth, false); retrieveEntityReq.setRequestHeader("Accept", "*/*"); retrieveEntityReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 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, '')); // p:<parameters Name In ssrs> :Is optional when you want to have parameter. var x = retrieveEntityReq.responseText.lastIndexOf("ReportSession="); var y = retrieveEntityReq.responseText.lastIndexOf("ControlID="); var ret = new Array(); ret[0] = retrieveEntityReq.responseText.substr(x + 14, 24); ret[1] = retrieveEntityReq.responseText.substr(x + 10, 32); return ret; },