Plugins 无法将实体类型的对象强制转换为ActivityParty类型

Plugins 无法将实体类型的对象强制转换为ActivityParty类型,plugins,dynamics-crm,Plugins,Dynamics Crm,我正在使用CRM online 2015的自定义插件,每次尝试从“Email.to”字段访问activityparty时,我都会收到 下面是我的代码的样子: public class PreCreate : Plugin { public PreCreate() : base(typeof(PreCreate)) { base.RegisteredEvents.Add(new Tuple<int, string, str

我正在使用CRM online 2015的自定义插件,每次尝试从“Email.to”字段访问activityparty时,我都会收到

下面是我的代码的样子:

public class PreCreate : Plugin
{
   public PreCreate()
        : base(typeof(PreCreate))
     {           
        base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Create", "email", new Action<LocalPluginContext>(ExecutePreEntityCreate)));

     }

   public void ExecutePreEntityCreate(LocalPluginContext localContext)
    {

      var target = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];

      using (var context = new XrmServiceContext(localContext.OrganizationService))
          {
                var email = target.ToEntity<Email>(); //The entity has the right values
                 var activityPartyList=email.To // here I see the exception

                 //If I use the following code:                         
                var activityParty = email.GetAttributeValue<EntityCollection>("to");
                 //I get an empty ActivityParty(empty Id)

          }
    }

}
公共类预创建:插件
{
公共预创建()
:基本(类型(预创建))
{           
Add(新元组(20,“创建”,“电子邮件”,新操作(ExecutePreEntityCreate));
}
public void ExecutePreEntityCreate(localpluginontext localContext)
{
var target=(实体)localContext.PluginExecutionContext.InputParameters[“target”];
使用(var context=new XrmServiceContext(localContext.OrganizationService))
{
var email=target.ToEntity();//实体具有正确的值
var activityPartyList=email.To//在这里我看到了异常
//如果我使用以下代码:
var activityParty=email.GetAttributeValue(“收件人”);
//我得到一个空的ActivityParty(空Id)
}
}
}

是否必须对activityparty类型进行一些初始化?

代码、字段电子邮件没有问题。要返回EntityCollection并获取您需要使用的内容,请使用:

var entityCollection = email.GetAttributeValue<EntityCollection>("to");
var entityCollection=email.GetAttributeValue(“to”);
这将为您提供需要转换为ActivityParty(entityCollection.entities)的实体集合。 要转换所需的实体,请执行以下操作:

foreach (var entityItem in entityCollection.Entities)
   {
       var ap = entityItem.ToEntity<ActivityParty>();
       //Here you will get the LogicalName in this case Lead
       // the Id and the name 
       var leadId = ap.PartyId.Id;
      //To get the Lead 
      var lead=context.LeadSet.FirstOrDefault(l => l.Id == leadId);            
   }
foreach(entityCollection.Entities中的var entityItem)
{
var ap=entityItem.ToEntity();
//在这里,您将获得本案例Lead中的LogicalName
//Id和名称
var leadId=ap.PartyId.Id;
//领先
var lead=context.LeadSet.FirstOrDefault(l=>l.Id==leadId);
}
foreach (var entityItem in entityCollection.Entities)
   {
       var ap = entityItem.ToEntity<ActivityParty>();
       //Here you will get the LogicalName in this case Lead
       // the Id and the name 
       var leadId = ap.PartyId.Id;
      //To get the Lead 
      var lead=context.LeadSet.FirstOrDefault(l => l.Id == leadId);            
   }