我正在使用由CrmSvcUtil生成的早期绑定实体,并且正在通过检索帐户实体来测试SDK:-
var account = myContext.AccountSet.FirstOrDefault(o => o.Id == Guid.Parse("..."));
(顺便说一句,有没有更简单的方法可以通过ID检索实体?!)
查看返回的帐户对象,我可以看到各种类型的属性OptionSetValue
(例如“ PreferredContactMethodCode”)。如何从此OptionSetValue对象获取实际项目?
类似地,有许多type属性EntityReference
,其中包含一个Id和LogicalName(我假定的实体名称)。我如何填充这样的属性-它是Get ...方法之一吗?是否必须分别调用这些关系,或者是否可以“预取”某些关系作为检索帐户实体的初始查询的一部分?
与各种IEnumerable <>属性(大概对应于1-M实体关系)相似,例如,IEnumerable类型的称为“ opportunity_customer_accounts”的属性。如何填充这些属性之一?并且(再次)必须将其作为单独的查询/方法调用来完成,还是可以“预取”?
取回
我不太确定检索操作可以简化多少,但是对于一条记录,上下文的用户可能会过大。因此,您可以使用IOrganizationService检索您直接知道的特定记录:
account = service.Retrieve("account", Guid.Parse("..."), new ColumnSet(true));
选项集标签
对于OptionSet标签,您可以在这里查看我的答案:如何使用crm sdk和C#从CRM 2011中实体的字段中获取选项集。
如果您需要一个实体上多个OptionSet的标签,则可能只需要检索一次该实体的元数据(http://srinivasrajulapudi.blogspot.com/2012/01/retrieve-entity-metadata-in-crm-2011.html) :
string entityName ="contact";
// Get the metadata for the currently list's entity
// This metadata is used to create a "Property Descriptor Collection"
RetrieveEntityRequest mdRequest = new RetrieveEntityRequest ( )
{ EntityFilters = EntityFilters.All,
LogicalName = entityName,
RetrieveAsIfPublished = false
};
// Execute the request
RetrieveEntityResponse entityResponse = ( RetrieveEntityResponse ) this.ServiceProxy.Execute ( mdRequest );
EntityMetadata entityData = entityResponse.EntityMetadata;
//To Get Option Set Data
var preferdList= ( entityData.Attributes.Where ( p => p.LogicalName == "preferredcontactmethodcode" ) ).ToList ( ).FirstOrDefault ( ); ;
if ( preferdList != null ) {
PicklistAttributeMetadata optionList= preferdList as PicklistAttributeMetadata;
OptionSetMetadata opValues= optionList.OptionSet;
foreach ( var op in opValues.Options ) {
preferedMethod.Add ( new OptionSet { Value = op.Value.Value, Text = op.Label.LocalizedLabels[0].Label.ToString() } );
}
}
EntityReference()设置EntityReference类型的字段:
account.primarycontact = new EntityReference("contact", Guide.Parse("..."));
如果它们具有值,并且您要求在其中填写该列ColumnSet()
,那么我不确定我是否理解您的问题。如果您想要的是完整记录,那么您需要对service.Retrieve(...)
记录进行处理。
相关实体(例如,business_customer_accounts)
这是OrganizationServiceContext
使使用起来更轻松的地方(https://msdn.microsoft.com/zh-cn/library/gg695791.aspx):
context.LoadProperty(contact,“ transactioncurrency_contact”);
//动态读取相关实体var currency = contact.GetRelatedEntity(“ transactioncurrency_contact”); Console.WriteLine(currency.GetAttributeValue(“ currencyname”));;
//静态读取相关实体var currencyStatic = contact.transactioncurrency_contact; Console.WriteLine(currencyStatic.CurrencyName);
如果您未使用,则OrganizationServiceContext
可以尝试使用QueryExpression使用LinkedEntities
,尽管我从来没有这样做来填充早绑定的实体,所以我不知道它是否有效(也许有人会在答案中添加注释。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句