【问题标题】:Pulling the Display name of an attribute from entity Metadata从实体元数据中提取属性的显示名称
【发布时间】:2021-05-04 15:48:42
【问题描述】:

我在尝试使用 C# 从 CRM 获取数据方面相当陌生,我正在尝试获取我在 CRM 上的所有属性的显示名称,当我尝试时,我得到了 Microsoft.Xrm.Sdk.Label 的结果,但它没有似乎无法直接获得该标签的价值,有人能指出我正确的方向吗?

using System;    
using Microsoft.Xrm.Tooling.Connector;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;


namespace CRM_MetaData_Download
{
    class Program
    {
        static void Main(string[] args)
        {
            try {
                var connectionString = @"AuthType = Office365; Url = https://CRMINFORMATION";
                CrmServiceClient conn = new CrmServiceClient(connectionString);

                IOrganizationService service;
                service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
                RetrieveEntityRequest retrieveEntityRequest = new RetrieveEntityRequest
                {
                    EntityFilters = EntityFilters.All,
                    LogicalName = "account"
                };
                RetrieveEntityResponse retrieveAccountEntityResponse = (RetrieveEntityResponse)service.Execute(retrieveEntityRequest);
                EntityMetadata AccountEntity = retrieveAccountEntityResponse.EntityMetadata;

                Console.WriteLine("Account entity attributes:");
                foreach (object attribute in AccountEntity.Attributes)
                {
                    AttributeMetadata a = (AttributeMetadata)attribute;
                    Console.WriteLine(a.LogicalName + "    " +
                        a.Description + "    " +
                        a.DisplayName + "    " +
                        a.EntityLogicalName + "    " +
                        a.SchemaName + "    " +
                        a.AttributeType + "    "
                        );
                }
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }
}

【问题讨论】:

    标签: c# dynamics-crm dynamics-crm-sdk


    【解决方案1】:

    由于 Dynamics CRM 支持多语言功能,因此将为每种语言存储显示名称标签。你可以像下面这样得到它:

    a.DisplayName.UserLocalizedLabel.Label
    

    【讨论】:

    • 请注意:某些实体和某些字段可以将 DisplayName 或 UserLocalizedLabel 设为 null(尤其是内部的),SchemaName 或 LogicalName 始终包含一个值,它们可以在 DisplayName 或 UserLocalizedLabel 的情况下用作默认值为空
    • 调查了 2 小时,您先生进来拯救一天!谢谢,我不敢相信我忽略了这一点......我希望你能享受你的整个生活!
    • @SamuelDague 谢谢。还请考虑上面 Guido 提出的观点
    • 是的,这是我注意到的第一件事,我试了一下{}catch{},如果它在抓取数据时遇到问题,我只需将信息转换为 NULL
    • 对不起,我以为我做到了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多