c# – 在Dynamics 2011 SDK中,当LookupAttributeMetadata.Target
使用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”的关系. 更新: >实体:汇总字段(汇总字段)字段:组织标识(organizationid) 更新: //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>"; } } } 解决方法
您是否尝试使用
RetrieveRelationshipRequest消息?
那将返回一个RetrieveRelationshipResponse类,它有一个RelationshipMetadata属性,它依次依赖关系,它将是一个OneToManyRelationshipMetadata或ManyToManyRelationshipMetadata.在OneToManyRelationshipMetadata类的属性中,您将找到ReferencingAttribute和ReferencedAttribute属性,这是您想要的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |