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.ServiceModel using 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>"; } } }
解决方法
那将返回一个RetrieveRelationshipResponse类,它有一个RelationshipMetadata属性,它依次依赖关系,它将是一个OneToManyRelationshipMetadata或ManyToManyRelationshipMetadata.在OneToManyRelationshipMetadata类的属性中,您将找到ReferencingAttribute和ReferencedAttribute属性,这是您想要的.