主要步骤如下:

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

第一,生成邮件记录

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;
},

【原创】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. C# winform中 窗体缩放自适应的方法(不同电脑/不同分辨率)

    C# winform中 窗体缩放自适应的方法(不同电脑/不同分辨率)   窗体缩放是一个困扰我多时的问题,为了解决这个问题,我从网上找了很多相关的资料,很多人说用Anchor和Dock属性,但是我试了 ...

  2. Python学习---django之ORM语法[对象关系映射]180124

    ORM语法[对象关系映射] ORM: 用面向对象的方式去操作数据库的创建表以及增删改查等操作. 优点:1 ORM使得我们的通用数据库交互变得简单易行,而且完全不用考虑该死的SQL语句.快速开发. 2 ...

  3. 文件属性及OS模块使用(IO入门2)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6422270.html 本文出自:[Edwin博客园] 文件属性及OS模块使用(IO入门2) 1. pytho ...

  4. 函数去抖(debounce)与 函数节流(throttle)

    以下场景往往由于事件频繁被触发,因而频繁执行DOM操作.资源加载等重行为,导致UI停顿甚至浏览器崩溃. 1. window对象的resize.scroll事件 2. 拖拽时的mousemove事件 3 ...

  5. BZOJ 1061 志愿者招募 最小费用流&&线性规划建模

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1061 题目大意: 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主 ...

  6. python __new__以及__init__

    @[深入Python]__new__和__init__ 1 2 3 4 5 6 7 8 class A(object):     def __init__(self):         print & ...

  7. hook与链表操作

    hook与链表的节点操作有相似之处: 链表的前后顺序相当于程序的执行流: 对链表节点的替换或插入相当于hook技术: 1.替换:用新的节点替换原来的节点: 2.前插入: 3.后插入: 修改原来的结构.

  8. POJ 1320 Street Numbers 【佩尔方程】

    任意门:http://poj.org/problem?id=1320 Street Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  9. 4、Dubbo-工程实践

    4.实践测试 4.1).提出需求 某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址: 我们现在 需要创建两个服务模块进行测试  测试预期结果: 订单服务web模块在A服务器,用户服务模块在 ...

  10. Asp.Net Core + Ocelot 网关搭建:负载均衡的配置

    前言   上一篇中简单做了一个网关Demo.本篇中也记录一下负载均衡的配置实现. 演示   首先开三个服务,端口分别为 60001,60003,60005,然后分别启动三个服务.接下来在ApiGate ...