C# · 12月 30, 2021

c# – 在Dynamics 2011 SDK中,当LookupAttributeMetadata.Targets为空时,如何确定目标实体?

使用CRM 2011 SDK(v5.0.10)我遇到一些问题,一些Lookup字段的目标没有填充,我希望有人可以帮助我确定在这些情况下确定引用实体的最佳方式.

具体来说,我通过此调用检索实体,属性和关系元数据:

var entityRequest = new RetrieveAllEntitiesRequest { RetrieveAsIfPublished = true,EntityFilters = EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships };var entityResponse = (RetrieveAllEntitiesResponse)_organizationService.Execute(entityRequest);return entityResponse.EntityMetadata.ToList();

稍后,在使用从该调用返回的EntityMetadata对象时,我检查Attributes集合和LookupAttributeMetadata对象,我尝试使用LookupAttributeMetadata对象的Targets属性来确定查找引用的实体.

但是,在某些情况下,LookupAttributeMetadata对象具有空Targets集合.例如,活动活动(逻辑名称:campaignactivity)实体将Service字段(逻辑名称:服务)定义为Lookup字段,但LookupAttributeMetadata对象中的Targets属性为空.

当我查看实体的Web UI自定义屏幕并打开服务字段时,类型部分显示类型:查找,目标记录类型:帐户,关系名称:campaignactivity_account.

这些信息来自哪里?

另请注意:在活动活动或帐户实体上没有名为“campaignactivity_account”的关系.

更新:
我正在运行Dynamics CRM 2011汇总8的库存安装(尽管我也在Rolloup 7上看到这一点).虽然“广告系列活动”是我在示例中使用的字段,但总共有14个这个问题,如下所列.我正在寻找一个通用的解决方案(对于一个一次性的解决方案),以避免在我的代码中有一堆if(entityName ==“rollupfield”& fieldName ==“organizationid”)…逻辑,因为我正在使用的实体和字段是由用户在运行时选择的,我不一定提前知道我将被交付.

>实体:汇总字段(汇总字段)字段:组织标识(organizationid)
>实体:汇总查询(goalrollupquery)字段:拥有用户(拥有用户)
>实体:进程日志(工作流日志)字段:关于(关于对象)
>实体:已保存的视图(userquery)字段:父查询(parentqueryid)
>实体:广告系列活动(campaignactivity)字段:服务(serviceid)
>实体:电子邮件搜索(emailsearch)字段:父(parentobjectid)
>实体:时区定义(timezonedeFinition)字段:组织(organizationid)
>实体:广告系列响应(campaignresponse)字段:服务(serviceid)
>实体:快速运动(批量操作)领域:服务(serviceid)
>实体:字段许可(fieldpermission)字段:组织标识(organizationid)
>实体:时区本地化名称(timezonelocalizedname)字段:组织(organizationid)
>实体:时区规则(timezonerule)字段:组织(organizationid)
>实体:审计(审计)字段:记录(objectid)
>实体:帖子(帖子)字段:关于对象(aboutobjectid)

更新:
以下控制台应用程序可用于重现此问题.

//Requires the following Referenses:// Microsoft.CSharp// Microsoft.IdentityModel// Microsoft.xrm.sdk// System// System.Core// System.Data// System.Runtime.Serialization// System.ServiceModelusing System;using System.Linq;using System.Net;using Microsoft.Xrm.Sdk;using Microsoft.Xrm.Sdk.Client;using Microsoft.Xrm.Sdk.Messages;using Microsoft.Xrm.Sdk.Metadata;namespace TargetlessLookupsPOC{ internal static class Program { private const string OrganizationServiceURL = “http://dynamicscrm1/DynamicsCRM1/XRMServices/2011/Organization.svc”; private static void Main(string[] args) { Console.WriteLine(“====== Authenticating “); var organizationServiceMngt = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(OrganizationServiceURL)); var authCred = new AuthenticationCredentials(); authCred.ClientCredentials.Windows.ClientCredential = new NetworkCredential(); IOrganizationService orgProxy = new OrganizationServiceProxy(organizationServiceMngt,authCred.ClientCredentials); Console.WriteLine(“====== Fetching All Entity Metadata “); var entityRequest = new RetrieveAllEntitiesRequest { RetrieveAsIfPublished = true,EntityFilters = EntityFilters.Entity | EntityFilters.Attributes | EntityFilters.Relationships }; var entityResponse = (RetrieveAllEntitiesResponse) orgProxy.Execute(entityRequest); Console.WriteLine(“====== Searching For Targetless Lookups “); foreach (var ent in entityResponse.EntityMetadata) { foreach (var field in ent.Attributes .OfType<LookupAttributeMetadata>() .Where(lookup => !lookup.Targets.Any())) { Console.WriteLine(“Entity: {0} ({1}),Field: {2} ({3}) (type: {4}) is targetless”,ent.DisplayName.LabelText(),ent.LogicalName,field.DisplayName.LabelText(),field.LogicalName,field.AttributeType); } } Console.WriteLine(“=========================== Done”); Console.WriteLine(“** Press any key to continue **”); Console.ReadKey(); } public static string LabelText(this Label label) { return (label != null && label.UserLocalizedLabel != null) ? label.UserLocalizedLabel.Label : “<no label>”; } }}解决方法 您是否尝试使用 RetrieveRelationshipRequest消息?

那将返回一个RetrieveRelationshipResponse类,它有一个RelationshipMetadata属性,它依次依赖关系,它将是一个OneToManyRelationshipMetadata或ManyToManyRelationshipMetadata.在OneToManyRelationshipMetadata类的属性中,您将找到ReferencingAttribute和ReferencedAttribute属性,这是您想要的.