zoukankan      html  css  js  c++  java
  • 【原创】CRM 2015/2016,SSRS 生成PDF文件,幷以附件的形式发送邮件

    主要步骤如下:

    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;
    },
  • 相关阅读:
    【CentOS 6.5】QtCreator启动时关于dbus-1的错误解决方法
    【CentOS 6.5】 Qt Creator 启动失败
    C# mvc3 mvc4 伪静态及IIS7.5配置
    【水果大全】快看,你属于哪种水果身材?
    一首《放下》最近火了!
    【情感励志】不必仰望别人,自己亦是风景
    java JDBC连接 Sqlserver 非默认的实例名问题
    后台:酒店客服订单管理.把订单编号:1290939 从取消状态恢复确认有房状态(待发单状态)
    PHP安装环境搭建
    表操作汇总(复制,删除,修改,插入,查询及数据库的复制)
  • 原文地址:https://www.cnblogs.com/bennylam/p/9999665.html
Copyright © 2011-2022 走看看