javascript 如何获取CRM 2011的所有实体字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21755094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:37:10  来源:igfitidea点击:

How to get all entity's fields CRM 2011?

c#javascriptdynamics-crm-2011entityfield

提问by ernikoss

I'm trying to get all entity's fields schema names without query to existing record. I haven't got any idea.

我试图在不查询现有记录的情况下获取所有实体的字段模式名称。我没有任何想法。

Also, How can I get it with QueryExpression, cause it retrieve all fields, which contain some info?

另外,如何使用 QueryExpression 获取它,导致它检索包含一些信息的所有字段?

Andrii, I'm trying to do something like this

Andrii,我正在尝试做这样的事情

var query = new QueryExpression {EntityName = "", ColumnSet = new ColumnSet(true)};
var retrieve = service.RetrieveMultiple(query);

回答by JTW

If you have the CRM SDK (http://www.microsoft.com/en-us/download/details.aspx?id=24004), you will find a utility called "Crmsvcutil.exe" in the SDK\Bin folder. This executable will generate classes that represent each of your CRM entities and their fields. You can then use the type.GetFields() method to derive the fields of these classes within your code. Once you have created these classes with the crmsvcutil, you can use LINQ queries to query your CRM data based on any criteria that suits your needs, including non null fields. The issue you will encounter with trying to use QueryExpression is that you must define the fields manually as shown below:

如果您有 CRM SDK ( http://www.microsoft.com/en-us/download/details.aspx?id=24004),您会在 SDK\Bin 文件夹中找到一个名为“Crmsvcutil.exe”的实用程序。此可执行文件将生成代表您的每个 CRM 实体及其字段的类。然后,您可以使用 type.GetFields() 方法在您的代码中派生这些类的字段。使用 crmsvcutil 创建这些类后,您可以使用 LINQ 查询根据满足您需要的任何条件(包括非空字段)查询您的 CRM 数据。您在尝试使用 QueryExpression 时会遇到的问题是您必须手动定义字段,如下所示:

QueryExpression query = new QueryExpression("contact");
query.ColumnSet.AddColumns("firstname", "lastname");
query.Criteria.AddFilter(filter1);

回答by Francis Ducharme

I was looking for something similar and ended up cooking a useful method as such:

我一直在寻找类似的东西,最终得出了一个有用的方法:

/// <summary>
/// Retrieves an entity's attributes.
/// </summary>
/// <param name="entityName">entity's name</param>
/// <param name="languageId">return display names of such language code</param>
/// <param name="xrm">xrmservicecontext object</param>
/// <returns>Dictionary<string, string></returns>
public static Dictionary<string, string> GetAttributes(string entityName, int languageId, XrmServiceContext xrm)
{
    Dictionary<string, string> attributesData = new Dictionary<string, string>();

    RetrieveEntityRequest metaDataRequest = new RetrieveEntityRequest();
    RetrieveEntityResponse metaDataResponse = new RetrieveEntityResponse();
    metaDataRequest.EntityFilters = EntityFilters.Attributes;
    metaDataRequest.LogicalName = entityName;
    metaDataResponse = (RetrieveEntityResponse)xrm.Execute(metaDataRequest);

    foreach (AttributeMetadata a in metaDataResponse.EntityMetadata.Attributes)
    {
        //if more than one label for said attribute, get the one matching the languade code we want...
        if (a.DisplayName.LocalizedLabels.Count() > 1)
            attributesData.Add(a.LogicalName, a.DisplayName.LocalizedLabels.Where(x=>x.LanguageCode == languageId).SingleOrDefault().Label);

        //else, get the only one available regardless of languade code...
        if (a.DisplayName.LocalizedLabels.Count() == 1)
            attributesData.Add(a.LogicalName, a.DisplayName.LocalizedLabels[0].Label);
    }

        return attributesData;
}

You could of course modify it to return whatever information you need about the entity's attributes, but I in my case I was looking for a way to replace the logical name of attributes that the CRM SDK uses to auto generate gridview column's with the actual display name of attributes.

您当然可以修改它以返回您需要的有关实体属性的任何信息,但在我的情况下,我正在寻找一种方法来替换 CRM SDK 用于使用实际显示名称自动生成 gridview 列的属性的逻辑名称的属性。

回答by Andrew Butenko

You should try to use RetrieveEntity messageto get all entity fields. You would not be able to get field in case it is equals to null. You will have to analyze - if attribute inside collection - it contains value, otherwise it is equals to null.

您应该尝试使用RetrieveEntity 消息来获取所有实体字段。如果字段等于空,您将无法获取字段。您将必须分析 - 如果集合内的属性 - 它包含值,否则等于 null。