因此,当您拥有实体名称时,尝试获取对象类型代码似乎是CRM中一个相当常见的问题。但由于我总是要以艰难的方式做事,所以我有相反的任务:我有对象类型代码,需要获取实体名称。
一般而言,最终的任务是将CRM与电子邮件中的信息联系起来,并使用它来查询CRM以获取更多信息。我可以轻松获得的有关ID和对象类型代码。对于标准实体,我可以使用硬编码查找来获取实体名称。但是对于自定义实体,这不会起作用,因为对象类型代码在不同的组织中可能不同。
获取relatedID和对象类型代码:
string regardingId;
regardingId = (String)(item.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/crmRegardingId"));
dynamic crmRegardingObjectType;
crmRegardingObjectType = item.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/crmRegardingObjectType");
我可以请求检索所有实体的元数据&找到合适的对象类型代码,但这确实很慢并且数据太多了:
RetrieveAllEntitiesRequest entitiesRequest = new RetrieveAllEntitiesRequest();
entitiesRequest.EntityFilters = EntityFilters.Entity;
RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)service.Execute(entitiesRequest);
我必须认为有更好的方法,但我已经被困了一段时间。
ANSWER 这就是我最终做的事情:
string entityLogicalName = String.Empty;
MetadataFilterExpression EntityFilter = new MetadataFilterExpression(LogicalOperator.And);
EntityFilter.Conditions.Add(new MetadataConditionExpression("ObjectTypeCode", MetadataConditionOperator.Equals, objectTypeCode));
MetadataPropertiesExpression mpe = new MetadataPropertiesExpression();
mpe.AllProperties = false;
mpe.PropertyNames.Add("DisplayName");
mpe.PropertyNames.Add("ObjectTypeCode");
mpe.PropertyNames.Add("PrimaryIdAttribute");
mpe.PropertyNames.Add("PrimaryNameAttribute");
EntityQueryExpression entityQueryExpression = new EntityQueryExpression()
{
Criteria = EntityFilter,
Properties = mpe
};
RetrieveMetadataChangesResponse initialRequest = GetMetadataChanges(entityQueryExpression, null, DeletedMetadataFilters.OptionSet);
if (initialRequest.EntityMetadata.Count == 1)
{
entityLogicalName = initialRequest.EntityMetadata[0].LogicalName;
}
return entityLogicalName;
protected RetrieveMetadataChangesResponse GetMetadataChanges(EntityQueryExpression entityQueryExpression, String clientVersionStamp, DeletedMetadataFilters deletedMetadataFilter)
{
RetrieveMetadataChangesRequest retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest()
{
Query = entityQueryExpression,
ClientVersionStamp = clientVersionStamp,
DeletedMetadataFilters = deletedMetadataFilter
};
return (RetrieveMetadataChangesResponse)organizationService.Execute(retrieveMetadataChangesRequest);
}
答案 0 :(得分:1)
自Rollup 12 for CRM 2011 Metadata query is available以来。因此,您可以尝试构建查询并仅获取一个实体,而不是查询所有实体。