主要步骤如下:

  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. 2.java相对路径与绝对路径

    1.基本概念的理解 绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz/test.txt 代表了test.txt文件的绝对路径.http://www. ...

  2. ORACLE 11GR2 安装时配置了域,后期删除

    因为用了一个安全平台.此平台居然不支持oracle中的服务吗有"."而这个点就是因为当时安装oracle录入了域.原来以为是修改服务名.百多了很多,最后发现就是删除域即可 感谢此文 ...

  3. EXCHANGE 2013 TLS传输层安全

    默认情况下,SMTP流量是不被加密的,这就导致在公网上进行邮件沟通就像是在广播一样,任何人拦截到该邮件都可以轻而易举的读取其内容.但是现实场景中有许多敏感信息是通过邮件来进行发送的,所以其中一种保护邮 ...

  4. linux 三大利器 grep sed awk sed

    sed主要内容和原理介绍 sed 流处理编辑器 sed一次处理一行内容,读入一行处理一行 sed不改变文件内容(除非重定向) sed 命令行格式 $ sed [options] 'command' f ...

  5. Mysql进阶-day3

    多实例介绍: mysql多实例就是一台服务器开启多个不同的服务端口(3306,3307),运行多个MySQL服务进程,这些服务进程通过不同的socket监听不同的服务端口来提供各自的服务端口. 这些m ...

  6. python3 邮件,多用户,抄送

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2017/8/19 10:44 # @Author : Lys # @Site : # ...

  7. 四种Timer的区别和用法

    1.System.Threading.Timer 线程计时器 1.最底层.轻量级的计时器.基于线程池实现的,工作在辅助线程. 2.它并不是内在线程安全的,并且使用起来比其他计时器更麻烦.此计时器通常不 ...

  8. codeforces 420D Cup Trick

    codeforces 420D Cup Trick 题意 题解 官方做法需要用到线段树+平衡树(? 如果数据小的话似乎可以用莫队).然后代码好长好长.我补了一个只要用到树状数组的做法. 代码 #inc ...

  9. 关于eclipse没有js、xml代码提示的解决:下载一个插件

    1)eclipse打开帮助 2)Eclipse Marketplace,然后搜索AngularJS Eclipse 安装后重启就行了 xml的搜索Rinzo. 没有vpn,我的网络到达不了.

  10. Echarts 嵌套饼图实现,内环点击控制外环显示

    Echarts有交互事件,但是如果用其进行图形变更,恐怕就只有重新修改配置了. 如图,我想要实现,内圈的数据控制外圈的图形,当点击内环的某一块时,就可以实现,更改外环的目的. 起初我想能不能直接修改o ...