主要步骤如下:

  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. CMD命令行下编译.Net Visual Studio 项目

    有时候我们需要编译.net 的sln解决方案,可是VS打开的速度太慢,可以用命令行进行代替,详细过程如下: 1.开始菜单——>Visual Studio 2017(根据你电脑上安装的VS版本来) ...

  2. 打通版微社区(1):PHP环境部署 for DZX3.2

    写在前面:本文参考了http://blog.sina.com.cn/s/blog_513be2630101linz.html非常感谢博主此文对我此次操作帮助很大.PHP的windows部署方案主要分为 ...

  3. 克隆对象__clone()方法

    有的时候我们需要在一个项目里面,使用两个或多个一样的对象,如果你使用“new”关键字重新创建对象的话,再赋值上相同的属性,这样做比较烦琐而且也容易出错,所以要根据一个对象完全克隆出一个一模一样的对象, ...

  4. [DP]洛谷P1115最大子段和

    题目来源 https://www.luogu.org/problemnew/show/P1115 题目描述 给出一段序列,选出其中连续且非空的一段使得这段和最大. 输入输出格式 输入格式: 第一行是一 ...

  5. PDF转WORD工具推荐:迅捷PDF转换器6.8版

    迅捷PDF转换器是一款功能强大的pdf转换成word转换器软件.它不仅支持pdf转换成word,word转pdf,还可以实现excel,ppt,jpg与pdf文件的一键式转换.有了它,你可以很轻松转换 ...

  6. Chapter 3 Top 10 List

    3.1 Introduction Given a set of (key-as-string, value-as-integer) pairs, then finding a Top-N ( wher ...

  7. Alpha Scrum7

    Alpha Scrum7 牛肉面不要牛肉不要面 Alpha项目冲刺(团队作业5) 各个成员在 Alpha 阶段认领的任务 林志松:项目发布 陈远军.陈彬:播放器各环境的测试 项目的发布说明 本版本的新 ...

  8. Centos7 之目录处理命令(八)

    linux中 关于目录 有几个重要概念 一个是 / 根目录  还有一个当前用户的家目录 比如 root用户的家目录是 /root  普通用户的家目录是/home/xxx 下 root登录 默认家目录 ...

  9. VIM之模式

    1.模式介绍: 在真正开始使用VIM之前,你必须先了解VIM的模式,否则在 VIM 面前你可能会手足无措.VIM是有模式 编辑器,这意味着 VIM 有多种不同的工作模式,在不同的工作模式下用户相同的操 ...

  10. 推荐一个WPF仪表盘开源控件

    前段时间,做服务器端监控系统,为了界面好看,采用WPF.硬件相关监控,比如CPU.内存等,想用仪表盘控件.网上找了很多这种控件,基本上都是第三方商业控件(虽然很漂亮,不过得money...).最后在C ...