如何从RegardingID的对象类型代码中获取CRM实体名称? [英] How to get the CRM Entity Name from the Object Type Code of a RegardingID?
问题描述
因此,当您拥有实体名称时,尝试获取对象类型代码似乎是CRM中一个相当普遍的问题.但是由于我总是必须用困难的方式做事,所以我有相反的任务:我有对象类型代码,并且需要获取实体名称.
So it seems that it is a fairly common question in CRM to try get the object type code when you have an entity name. But since I always have to do things the hard way, I have the reverse task: I have the object type code, and need to get the entity name.
通常,最终任务是获取与电子邮件中的信息有关的CRM,并使用它来查询CRM以获得其他信息.我可以很容易地获得AboutID和对象类型代码.对于标准实体,我可以使用硬编码查找来获取实体名称.但是对于自定义实体,这是行不通的,因为对象类型代码在不同的组织中可能不同.
In general, the ultimate task is to take the the CRM regarding information on an email, and use it to query CRM for additional info. I can get the regardingID and the object type code easily. And for standard entities I can use a hardcoded lookup to get an entity name. But for custom entities, this won't work, as the object type code can be different in different organizations.
要获取AboutID和对象类型代码:
To get the regardingID and object type code:
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");
我可以请求检索所有实体的元数据&找到合适的对象类型代码,但这确实很慢,而且数据太多:
I could do a request to retrieve metadata for all entities & find the appropriate object type code, but this is really slow and way too much data:
RetrieveAllEntitiesRequest entitiesRequest = new RetrieveAllEntitiesRequest();
entitiesRequest.EntityFilters = EntityFilters.Entity;
RetrieveAllEntitiesResponse response = (RetrieveAllEntitiesResponse)service.Execute(entitiesRequest);
我不得不认为有更好的方法,但是我被困了一段时间.
I have to think there is a better way, but I have been stuck for a while.
答案 这就是我最终要做的:
ANSWER This is what I ended up doing:
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);
}
推荐答案
Since Rollup 12 for CRM 2011 Metadata query is available. So instead of querying of all entities you can try to build query and get only one entity.
这篇关于如何从RegardingID的对象类型代码中获取CRM实体名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!