CRM工作流程向用户发送报告附件

时间:2017-11-22 09:00:43

标签: reporting-services dynamics-crm crm dynamics-crm-online

我编写了一些代码,通过CRM向特定用户发送电子邮件附件,使用工作流程,附件是一份成绩单报告,其中包含2个子报告,以获取有关学生的一些数据。我遇到的问题是电子邮件没有发送,附件中断表示:

  

“子报告的数据检索失败,'Subreport2',位于:   / StudentReports / StudentAddress。请查看日志文件以获取更多信息   信息“。

日志文件不包含任何有用信息。这是代码:

using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.Workflow;
using NUP.CRM.DAL;
using NUP.CRM.DAL.Utility;
using NUP.CRM.Utilities;
using System.Collections.Generic;
using System.Workflow.ComponentModel;



namespace NUP.CRM.WorkFlows

{
    /// <summary>
    /// Sends transcript of a given assessment board and ac. year as attachment to email
    /// </summary>
    [CrmWorkflowActivity("Send email with Report Attached", "NUP")]
    public class SendReport : Activity
    {
        public static DependencyProperty AcademicYearProperty = DependencyProperty.Register("AcademicYear", typeof(string), typeof(SendReport));
        public static DependencyProperty AssBoardProperty = DependencyProperty.Register("AssBoard", typeof(string), typeof(SendReport));
        public static DependencyProperty EmailAddressTypeProperty = DependencyProperty.Register("EmailAddressType", typeof(string), typeof(SendReport));
        public static DependencyProperty EmailBodyProperty = DependencyProperty.Register("EmailBody", typeof(string), typeof(SendReport));
        public static DependencyProperty IsPositiveProperty = DependencyProperty.Register("IsPositive", typeof(CrmBoolean), typeof(SendReport));
        public static DependencyProperty ReportNameProperty = DependencyProperty.Register("ReportName", typeof(string), typeof(SendReport));
        public static DependencyProperty ReportPathProperty = DependencyProperty.Register("ReportPath", typeof(string), typeof(SendReport));
        public static DependencyProperty SenderProperty = DependencyProperty.Register("Sender", typeof(Lookup), typeof(SendReport));
        public static DependencyProperty SendFromAddressProperty = DependencyProperty.Register("SendFromAddress", typeof(string), typeof(SendReport));
        public static DependencyProperty SubjectProperty = DependencyProperty.Register("Subject", typeof(string), typeof(SendReport));

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            IWorkflowContext context = executionContext.GetService<IContextService>().Context;
            ICrmService service = context.CreateCrmService();

            byte[] report = ReportUtility.GetReport(
                ReportPath,
                ReportUtility.Formats.PDF,
                new string[] { "contactid", "AcYear", "IsPositive", "AssBoard" },
                new string[] {
                         context.PrimaryEntityId.ToString(),
                         AcademicYear.ToString(),
                         IsPositive.Value.ToString(),
                         AssBoard }
                );


            string addressToUse = DynamicEntityUtility.GetContactEmailOfType(service, context.PrimaryEntityId, EmailAddressType);

            EmailUtility emailUtility =
                new EmailUtility(service, context.UserId, context.PrimaryEntityId, addressToUse);

            List<EmailUtility.Attachment> attachments =
                new List<EmailUtility.Attachment>()
            {
                    new EmailUtility.Attachment(ReportName, report)
                };

            emailUtility.Send(Subject, attachments);

            return ActivityExecutionStatus.Closed;

        }

        [CrmInput("AcademicYear")]
        public string AcademicYear
        {
            get
            {
                return (string)base.GetValue(AcademicYearProperty);
            }
            set
            {
                base.SetValue(AcademicYearProperty, value);
            }
        }

        [CrmInput("AssBoard")]
        public string AssBoard
        {
            get
            {
                return (string)base.GetValue(AssBoardProperty);
            }
            set
            {
                base.SetValue(AssBoardProperty, value);
            }
        }

        [CrmInput("EmailAddressType")]
        public string EmailAddressType
        {
            get
            {
                return (string)base.GetValue(EmailAddressTypeProperty);
            }
            set
            {
                base.SetValue(EmailAddressTypeProperty, value);
            }
        }

        [CrmInput("EmailBody")]
        public string EmailBody
        {
            get
            {
                return (string)base.GetValue(EmailBodyProperty);
            }
            set
            {
                base.SetValue(EmailBodyProperty, value);
            }
        }

        [CrmInput("IsPositive")]
        public CrmBoolean IsPositive
        {
            get
            {
                return (CrmBoolean)base.GetValue(IsPositiveProperty);
            }
            set
            {
                base.SetValue(IsPositiveProperty, value);
            }
        }

        [CrmInput("ReportName")]
        public string ReportName
        {
            get
            {
                return (string)base.GetValue(ReportNameProperty);
            }
            set
            {
                base.SetValue(ReportNameProperty, value);
            }
        }

        [CrmInput("ReportPath")]
        public string ReportPath
        {
            get
            {
                return (string)base.GetValue(ReportPathProperty);
            }
            set
            {
                base.SetValue(ReportPathProperty, value);
            }
        }

        [CrmReferenceTarget("systemuser"), CrmInput("Sender")]
        public Lookup Sender
        {
            get
            {
                return (Lookup)base.GetValue(SenderProperty);
            }
            set
            {
                base.SetValue(SenderProperty, value);
            }
        }

        [CrmInput("SendFromAddress")]
        public string SendFromAddress
        {
            get
            {
                return (string)base.GetValue(SendFromAddressProperty);
            }
            set
            {
                base.SetValue(SendFromAddressProperty, value);
            }
        }

        [CrmInput("Subject")]
        public string Subject
        {
            get
            {
                return (string)base.GetValue(SubjectProperty);
            }
            set
            {
                base.SetValue(SubjectProperty, value);
            }
        }
    }
}

0 个答案:

没有答案