Показать сообщение отдельно
Старый 06.08.2009, 12:26   #48  
moskalevas is offline
moskalevas
Участник
 
107 / 11 (1) +
Регистрация: 16.07.2009
Создал проект Class Project, добавил необходимые References, как и в примере, добавил код:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Workflow;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.ServiceModel;
using System.Security.Principal;

namespace SendReportAction
{
[CrmWorkflowActivity("Execute and send a report")]
public class SendReport : SequenceActivity
{
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{

if (MailRecipient != null && !MailRecipient.IsNull && !MailRecipient.IsNullSpecified)
{
//создание email***************************************************************************************
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext workflowContext = contextService.Context;
ICrmService crmservice = workflowContext.CreateCrmService();
email mail = new email();

activityparty fromparty = new activityparty();
fromparty.partyid = new Lookup();
fromparty.partyid.type = EntityName.systemuser.ToString();
fromparty.partyid.Value = workflowContext.UserId;
mail.from = new activityparty[] { fromparty };

activityparty toparty = new activityparty();
toparty.partyid = new Lookup();
toparty.partyid.type = EntityName.systemuser.ToString();
toparty.partyid.Value = workflowContext.UserId;
mail.to = new activityparty[] { toparty };
mail.subject = "Report Subscription";
mail.sender = "babayan@mail.ru";
mail.description = "Report Subscription";
mail.ownerid = new Owner();
mail.ownerid.type = EntityName.systemuser.ToString();
mail.ownerid.Value = workflowContext.UserId;
Guid createdEmailGuid = crmservice.Create(mail);
//*****************************************************************************************************

//формирование и экспорт отчёта************************************************************************
Reporting.SessionHeader sessionheader = null;
byte[] result;
string encoding;
string mimetype;
Reporting.ParameterValue[] parametersUsed = null;
Reporting.Warning[] warnings;
string[] streamids;
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Transport.Realm = string.Empty;
EndpointAddress endpoint = new EndpointAddress(ServiceURL);

Reporting.ReportingServiceSoapClient client = new Reporting.ReportingServiceSoapClient(binding, endpoint);
client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;

client.Render(ref sessionheader, ReportName, "Excel", null, null, null, null, null, out result,
out encoding, out mimetype, out parametersUsed, out warnings, out streamids);
//*****************************************************************************************************

//прикрепление отчёта к письму*************************************************************************
activitymimeattachment attach = new activitymimeattachment();
attach.activityid = new Lookup(EntityName.email.ToString(), createdEmailGuid);
attach.body = System.Convert.ToBase64String(result);
attach.subject = "Report Subscription";
attach.filename = "Report.xls";
attach.filesize = new CrmNumber(result.Length);
attach.mimetype = @"application/vnd.ms-excel";
crmservice.Create(attach);
//*****************************************************************************************************

//отправка письма**************************************************************************************
SendEmailRequest sendrequest = new SendEmailRequest();
sendrequest.EmailId = createdEmailGuid;
sendrequest.TrackingToken = "";
sendrequest.IssueSend = true;
crmservice.Execute(sendrequest);
}
return ActivityExecutionStatus.Closed;
}

//объявляем URL of Reporting Service
public static DependencyProperty ServiceURLProperty = DependencyProperty.Register("ServiceURL", typeof(string),
typeof(SendReport));
[CrmInput("ServiceURL")]
public string ServiceURL
{
get
{
return (string)base.GetValue(ServiceURLProperty);
}
set
{
base.SetValue(ServiceURLProperty, value);
}
}

//объявляем ReportName
public static DependencyProperty ReportNameProperty = DependencyProperty.Register("ReportName", typeof(string),
typeof(SendReport));
[CrmInput("ReportName")]
public string ReportName
{
get
{
return (string)base.GetValue(ReportNameProperty);
}
set
{
base.SetValue(ReportNameProperty, value);
}
}

//объявляем MailRecipient (получатель письма)
public static DependencyProperty MailRecipientProperty = DependencyProperty.Register("MailRecipient", typeof(Lookup),
typeof(SendReport));
[CrmInput("MailRecipient")]
[CrmReferenceTarget("systemuser")]
public Lookup MailRecipient
{
get
{
return (Lookup)base.GetValue(MailRecipientProperty);
}
set
{
base.SetValue(MailRecipientProperty, value);
}
}
}
}


Далее застронгнеймил сборку и опубликовал. В Dynamics CRM создаю бизнес-процесс. Объект - ЗАКАЗ. В ServiceURL:http://[назва_сервера]/Reportserver/ReportService.asmx, ReportName:/Новая папка/Прайс-лист, MailRecipient:[имя получателя]. Опубликовал бизнес-процесс. На форме ЗАКАЗ появилась кнопка Выполнить бизнес-процесс, но e-mail не отсылается.