主要步骤如下:

  1. 生成一条邮件记录
  2. 生成一条ActivityParty记录
  3. 生成PDF文件,并以Base64添加到ActivityMimeAttachment 中去
  4. 打开发送邮件窗口,以便编辑及发送邮件

第一,生成邮件记录

  1. CreateEmail: function () {
  2.   var email = new Object();
  3.   email.Subject = "Quotation";
  4.   SDK.REST.createRecord(email, "Email", EmailCallBack, function (error) { alert(error.message); });
  5. },

第二,当成功建好邮件记录后,我们来生成ActivityParty记录

  1. // Email Call Back function
  2. EmailCallBack: function (result) {
  3.   email1 = result;
  4.   var activityParty = new Object();
  5.   // Set the "party" of the ActivityParty // EntityReference of an entity this activityparty relatated to.
  6.   activityParty.PartyId = {
  7.     Id: Xrm.Page.context.getUserId(), // id of the the current user which becomes the sender
  8.     LogicalName: "systemuser"
  9.   };
  10.   // Set the "activity" of the ActivityParty
  11.   // EntityReference.
  12.   activityParty.ActivityId = {
  13.     Id: result.ActivityId,
  14.     LogicalName: "email"
  15.   };
  16.   // Set the participation type (what role the party has on the activity).
  17.   activityParty.ParticipationTypeMask = { Value: 1 }; // 1 mean Sender
  18.  
  19.   SDK.REST.createRecord(activityParty, "ActivityParty", ActivityPartyCallBack, function (error) { alert(error.message); });
  20. },

第三,当成功建好ActivityParty记录后,调用SSRS生成PDF形式的报表,并以附件形式附加到邮件中去

  1. ActivityPartyCallBack: function (result2) {
  2.   // Generate the pdf file to attached to the Email.
  3.   var responseSession = getReportingSession();
  4.   encodePdf(responseSession);
  5. },
  1. // create a Email record with the attachement and other parameters.
  2. CreateEmailAttachment: function (bdy) {
  3.   //Email attachment parameters
  4.   var activitymimeattachment = Object();
  5.   activitymimeattachment.ObjectId = Object();
  6.   activitymimeattachment.ObjectId.LogicalName = "email";
  7.   activitymimeattachment.ObjectId.Id = email1.ActivityId;
  8.   activitymimeattachment.ObjectTypeCode = "email",
  9.   activitymimeattachment.Subject = "File Attachment";
  10.   activitymimeattachment.Body = bdy;
  11.   activitymimeattachment.FileName = "Quotation.pdf";
  12.   //Attachment call
  13.   activitymimeattachment.MimeType = "application/pdf";
  14.  
  15.   SDK.REST.createRecord(activitymimeattachment, "ActivityMimeAttachment",ActivityMimeAttachmentCallBack, function (error) { alert(error.message); });
  16. },

第四,打开邮件编辑窗口,以便编辑及发送邮件。

  1. ActivityMimeAttachmentCallBack: function (result) {
  2.   var options = {
  3.     openInNewWindow: true
  4.   };
  5.   Xrm.Utility.openEntityForm("email", email1.ActivityId, null, options);
  6. },

以下是SSRS生成PDF的代码。

  1. //Encode the binary output pdf file to create an attachement
  2. encodePdf: function (responseSession) {
  3.  
  4.   var retrieveEntityReq = new XMLHttpRequest();
  5.  
  6.   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";
  7.  
  8.   retrieveEntityReq.open("GET", pth, true);
  9.   retrieveEntityReq.setRequestHeader("Accept", "*/*");
  10.   retrieveEntityReq.responseType = "arraybuffer";
  11.  
  12.   retrieveEntityReq.onreadystatechange = function () {
  13.     if (retrieveEntityReq.readyState == 4 && retrieveEntityReq.status == 200) {
  14.       var binary = "";
  15.       var bytes = new Uint8Array(this.response);
  16.  
  17.       for (var i = 0; i < bytes.byteLength; i++) {
  18.         binary += String.fromCharCode(bytes[i]);
  19.       }
  20.       var bdy = btoa(binary);
  21.  
  22.       CreateEmailAttachment(bdy);
  23.     }
  24.   };
  25.   retrieveEntityReq.send();
  26. },
  1. getReportingSession: function () {
  2.   var selectedIds = Xrm.Page.data.entity.getId();
  3.   var reportName = "quotation.rdl";
  4.   var reportGuid = // Report GUID - Replace with your report GUID
  5.  
  6.   var pth = Xrm.Page.context.getClientUrl() + "/CRMReports/rsviewer/QuirksReportViewer.aspx";
  7.  
  8.   var retrieveEntityReq = new XMLHttpRequest();
  9.  
  10.   retrieveEntityReq.open("POST", pth, false);
  11.  
  12.   retrieveEntityReq.setRequestHeader("Accept", "*/*");
  13.  
  14.   retrieveEntityReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  15.  
  16.   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, ''));
  17.   // p:<parameters Name In ssrs> :Is optional when you want to have parameter.
  18.   var x = retrieveEntityReq.responseText.lastIndexOf("ReportSession=");
  19.   var y = retrieveEntityReq.responseText.lastIndexOf("ControlID=");
  20.  
  21.   var ret = new Array();
  22.  
  23.   ret[0] = retrieveEntityReq.responseText.substr(x + 14, 24);
  24.   ret[1] = retrieveEntityReq.responseText.substr(x + 10, 32);
  25.  
  26.   return ret;
  27. },

【原创】CRM 2015/2016,SSRS 生成PDF文件,幷以附件的形式发送邮件的更多相关文章

  1. 【原创】Dynamics CRM 2015/2016,以PDF的形式打开SSRS报表。

    基本步骤: 使用SSRS建立报表,以下的例子是以记录的GUID作为报表的参数 获取ReportSession 和 ControlId来调用报表 以PDF的形式预览报表 一.根据报表的名称获取报表的GU ...

  2. linux下编译bib、tex生成pdf文件

    实验: 在linux环境下,编译(英文)*.bib和*.tex文件,生成pdf文件. 环境: fedora 20(uname -a : Linux localhost.localdomain 3.19 ...

  3. ThinkPHP3.2.3扩展之生成PDF文件(MPDF)

    目前是PHP生成PDF文件最好的插件了,今天介绍下在ThinkPHP3.2.3里如何使用. 先安照路径放好如图. 下面是使用方法 public function pdf(){ //引入类库 Vendo ...

  4. [轉載]史上最强php生成pdf文件,html转pdf文件方法

    之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类.方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有 ...

  5. asp.net生成PDF文件 (1)

    asp.net生成PDF文件 (1) 这个是例子是网上淘来的,哈哈,很有用的! 首先要到网上下载itextsharp.dll,然后添加引用,主程序如下: 1 2 3 4 5 6 7 8 9 10 11 ...

  6. 怎么用PHP在HTML中生成PDF文件

    原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PH ...

  7. JAVA生成PDF文件

    生成PDF文件是主要应用的是ITEXT插件 import java.awt.Color; import java.io.File; import java.io.FileOutputStream; i ...

  8. .NET生成PDF文件

    C#未借助第三方组件,自己封装通用类,生成PDF文件. 调用方式: //路径 string path = @"C:\yuannwu22.pdf"; //内容 string strC ...

  9. 史上最强php生成pdf文件,html转pdf文件方法

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

随机推荐

  1. JDK1.9怎么配置环境变量

  2. 自创open vp n windows步骤

    Easy Windows Guide¶ This page contains a no-frills guide to getting OpenVPN up and running on a Wind ...

  3. 从golang-gin-realworld-example-app项目学写httpapi (六)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/validators.go 验证器 ...

  4. java "Too small initial heap" 错误

    Tomcat内存配置 JAVA_OPTS="-server -Duser.timezone=GMT+08-Xms1024m -Xmx1024m -XX:PermSize=1024m -Xmn ...

  5. Hadoop配置文件详解

    1       获取默认配置 配置hadoop,主要是配置core-site.xml,hdfs-site.xml,mapred-site.xml三个配置文件,默认下来,这些配置文件都是空的,所以很难知 ...

  6. Linux 系统性能监控命令详解

    Linux 系统性能监控命令详解 CPU MEMORY IO NETWORK LINUX进程内存占用查看方法 系统负载过重时往往会引起其它子系统的问题,比如:->大量的读入内存的IO请求(pag ...

  7. SQL的别名和SQL的执行顺序和SQL优化

    SQL的别名 1.不可以在where子句中使用列名的别名,即select name t from emp where t>2999;是不允许的 2.使用别名的好处: 提高SQL的易读性 提高SQ ...

  8. 最大传输单元MTU

    http://baike.baidu.com/link?url=mU41JFjZzOb3R5crQFCNdocT5ovAswcoIqL2A4U6O5Re_U0-HIYndHG0vSKwc6HbptvH ...

  9. session.upload_progress.enabled开启的问题

    exp.php的内容,存在文件包含 <?php include($_GET['lfi']); $key = ini_get("session.upload_progress.prefi ...

  10. Word 2019 for mac更新喽!

    新的Word2019添加了许多的新功能,提供了新的文档处理方式,如改进的数字笔功能.焦点模式.学习工具和翻译,为用户提供了用于创建专业而优雅的高效文档工具,帮助用户节省时间,并得到优雅美观的结果,有效 ...