在Workflow E-mail中添加注释
在CRM如何轻易克服的一些限制,在发送电子邮件的活动中,加载注释到电子邮件后面。
我在我的工作流中做了一个新的activity,使用到以下代码。

Code
using System;
using System.Workflow.ComponentModel;
using System.Workflow.Activities;
using System.Web.Services.Protocols;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.Workflow;
using System.Collections;
using System.Text;
namespace SonomaPartners.Crm.Workflow.Utilities
{
[CrmWorkflowActivity("Format Notes", "Utilities")]
public partial class FormatNotes : SequenceActivity
{
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext ctx = contextService.Context;
ICrmService service = ctx.CreateCrmService();
this.FormattedNotes = BuildNotesString(service, ctx.PrimaryEntityId);
return base.Execute(executionContext);
}
public static DependencyProperty FormattedNotesProperty = DependencyProperty.Register("FormattedNotes", typeof(string), typeof(FormatNotes));
[CrmOutput("Formatted Notes")]
public string FormattedNotes
{
get { return (string)base.GetValue(FormattedNotesProperty); }
set { base.SetValue(FormattedNotesProperty, value); }
}
private string BuildNotesString(ICrmService service, Guid id)
{
StringBuilder results = new StringBuilder();
results.Append("Notes:<br>");
BusinessEntityCollection notes = new BusinessEntityCollection();
notes = RetrieveNotesForId(service, id);
foreach (annotation note in notes.BusinessEntities)
{
results.Append(string.Format("{0}<br>", note.subject));
results.Append(string.Format("{0}<br><br>", note.notetext));
}
return results.ToString();
}
private BusinessEntityCollection RetrieveNotesForId(ICrmService service, Guid id)
{
QueryByAttribute query = new QueryByAttribute();
query.EntityName = "annotation";
query.ColumnSet = new ColumnSet(new String[] { "subject", "notetext", "createdby", "createdon" });
query.Attributes = new String[] { "objectid" };
query.Values = new Object[] { id };
query.Orders = new ArrayList();
query.Orders.Add(new OrderExpression("createdon", OrderType.Descending));
try
{
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = query;
RetrieveMultipleResponse returnedNotes = (RetrieveMultipleResponse)service.Execute(request);
return returnedNotes.BusinessEntityCollection;
}
catch (SoapException ex)
{
throw new Exception(ex.Detail.InnerText);
}
}
}
}
我在我的工作流中做了一个新的activity,使用到以下代码。
using System;
using System.Workflow.ComponentModel;
using System.Workflow.Activities;
using System.Web.Services.Protocols;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.Workflow;
using System.Collections;
using System.Text;
namespace SonomaPartners.Crm.Workflow.Utilities
{
[CrmWorkflowActivity("Format Notes", "Utilities")]
public partial class FormatNotes : SequenceActivity
{
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext ctx = contextService.Context;
ICrmService service = ctx.CreateCrmService();
this.FormattedNotes = BuildNotesString(service, ctx.PrimaryEntityId);
return base.Execute(executionContext);
}
public static DependencyProperty FormattedNotesProperty = DependencyProperty.Register("FormattedNotes", typeof(string), typeof(FormatNotes));
[CrmOutput("Formatted Notes")]
public string FormattedNotes
{
get { return (string)base.GetValue(FormattedNotesProperty); }
set { base.SetValue(FormattedNotesProperty, value); }
}
private string BuildNotesString(ICrmService service, Guid id)
{
StringBuilder results = new StringBuilder();
results.Append("Notes:<br>");
BusinessEntityCollection notes = new BusinessEntityCollection();
notes = RetrieveNotesForId(service, id);
foreach (annotation note in notes.BusinessEntities)
{
results.Append(string.Format("{0}<br>", note.subject));
results.Append(string.Format("{0}<br><br>", note.notetext));
}
return results.ToString();
}
private BusinessEntityCollection RetrieveNotesForId(ICrmService service, Guid id)
{
QueryByAttribute query = new QueryByAttribute();
query.EntityName = "annotation";
query.ColumnSet = new ColumnSet(new String[] { "subject", "notetext", "createdby", "createdon" });
query.Attributes = new String[] { "objectid" };
query.Values = new Object[] { id };
query.Orders = new ArrayList();
query.Orders.Add(new OrderExpression("createdon", OrderType.Descending));
try
{
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = query;
RetrieveMultipleResponse returnedNotes = (RetrieveMultipleResponse)service.Execute(request);
return returnedNotes.BusinessEntityCollection;
}
catch (SoapException ex)
{
throw new Exception(ex.Detail.InnerText);
}
}
}
}
浙公网安备 33010602011771号