Microsoft CRM 4.0 LINQ - 使用LINQ从CRM获取属性显示名称

时间:2011-06-20 18:54:38

标签: xml linq dynamics-crm-4

有没有人使用Xrm Advanced Dev Extension做到这一点?我正在创建一个外部门户

假设我在帐户实体中有一个名为aaa_accountfield的属性。帐户实体XML具有

<displaynames>
 <displayname description="attribute display name" languagecode="1033" />
 <displayname description="attribute display name in some other language" languagecode="1045" />
</displaynames>

提前致谢

1 个答案:

答案 0 :(得分:0)

不确定它与XML有什么关系,但我很确定你可以使用ADX获得一个能够发出元数据请求的IOrganizationService。我会给出以下代码。您应该修改它以检查不同的语言在获取第一个之前是否返回值,否则您将容易受到例外的影响。

 var crm = new Xrm.XrmDataContext("CacheDisabled");
 crm.UsingService(service =>
        {
                // Use this code to grab a complete set of EntityMetadata.
                var entityRequest = new RetrieveEntityRequest();
                entityRequest.LogicalName = "account";
                entityRequest.RetrieveAsIfPublished = false;

                RetrieveEntityResponse entityResponse = new RetrieveEntityResponse();
                EntityMetadata entityMetadata = entityResponse.EntityMetadata;
                //entityMetadata.Attributes...

                // Use this to grab AttributeMetadata for a specific attribute.
                RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest();
                attributeRequest.EntityLogicalName = "account";
                attributeRequest.LogicalName = "name";
                attributeRequest.RetrieveAsIfPublished = false;

                RetrieveAttributeResponse attributeResponse = new RetrieveAttributeResponse();
                AttributeMetadata attributeMetadata = attributeResponse.AttributeMetadata;
                string labelLanguage1 = attributeMetadata.DisplayName.LocLabels.Where(l => l.LanguageCode.Value == 1033).First().Label;
                string labelLanguage2 = attributeMetadata.DisplayName.LocLabels.Where(l => l.LanguageCode.Value == 1045).First().Label;

        }