Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取所有实体';s fields CRM 2011?_C#_Javascript_Dynamics Crm 2011_Entity_Field - Fatal编程技术网

C# 如何获取所有实体';s fields CRM 2011?

C# 如何获取所有实体';s fields CRM 2011?,c#,javascript,dynamics-crm-2011,entity,field,C#,Javascript,Dynamics Crm 2011,Entity,Field,我试图在不查询现有记录的情况下获取所有实体的字段模式名。我不知道 另外,我如何使用QueryExpression获取它,因为它检索所有包含一些信息的字段 安德烈,我正试着这样做 var query = new QueryExpression {EntityName = "", ColumnSet = new ColumnSet(true)}; var retrieve = service.RetrieveMultiple(query); 您应该尝试使用获取所有实体字段。若字段等于null,则无

我试图在不查询现有记录的情况下获取所有实体的字段模式名。我不知道

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

安德烈,我正试着这样做

var query = new QueryExpression {EntityName = "", ColumnSet = new ColumnSet(true)};
var retrieve = service.RetrieveMultiple(query);
您应该尝试使用获取所有实体字段。若字段等于null,则无法获取该字段。您必须分析-如果集合中的属性-它包含值,否则它等于null。

如果您有CRM SDK(),您将在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);

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

/// <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;
}
//
///检索实体的属性。
/// 
///实体名称
///返回此类语言代码的显示名称
///xrmservicecontext对象
///字典
公共静态字典GetAttributes(字符串entityName、int languageId、XrmServiceContext、xrm)
{
字典属性data=新字典();
RetrieveEntityRequest metaDataRequest=新的RetrieveEntityRequest();
RetrieveEntityResponse metaDataResponse=新的RetrieveEntityResponse();
metaDataRequest.EntityFilters=EntityFilters.Attributes;
metaDataRequest.LogicalName=entityName;
metaDataResponse=(RetrieveEntityResponse)xrm.Execute(metaDataRequest);
foreach(metaDataResponse.EntityMetadata.Attributes中的AttributeMetadata a)
{
//如果所述属性有多个标签,请获取与所需语言代码匹配的标签。。。
如果(a.DisplayName.LocalizedLabels.Count()>1)
attributesData.Add(a.LogicalName,a.DisplayName.LocalizedLabels.Where(x=>x.LanguageCode==languageId.SingleOrDefault().Label);
//否则,无论语言代码如何,都只获取一个可用的。。。
if(a.DisplayName.LocalizedLabels.Count()==1)
attributesData.Add(a.LogicalName,a.DisplayName.LocalizedLabels[0].Label);
}
返回属性数据;
}

当然,您可以对其进行修改,以返回您需要的有关实体属性的任何信息,但就我而言,我正在寻找一种方法,将CRM SDK用于自动生成gridview列的属性的逻辑名称替换为属性的实际显示名称。

Andrii,没有办法得到他们吗?你需要这些信息干什么?如果你想要实体的元数据,Andrii的答案是正确的,如果你想在不查询CRM的情况下获取字段名,woogy的答案是有效的。Daryl,我需要获取该实体的所有字段名,但是这个方法不能获取值等于null的字段。我需要实时获取所有字段的名称,以创建一个带有复选框的列表。woogy,我可能创建了这些类,但我不能使用它们,因为字段可以稍后创建。这是正确的。为了解决这个问题,我保存了生成这些类的crmsvcutil命令,当我需要将这些类与CRM中的字段更改同步时,我可以重新运行。woogy,有一个商业项目出售。我不认为客户将来会这样做:)