【问题标题】:MS CRM 2011 FetchxmlMS CRM 2011 Fetchxml
【发布时间】:2013-10-30 19:24:34
【问题描述】:

从 Microsoft Dynamics CRM 2011 中的任何实体获取所有属性的通用 Fetch XML?

【问题讨论】:

  • 抱歉问题不清楚。现在我更新了问题。请参考我的答案以获得解决方案。

标签: fetchxml


【解决方案1】:

如果你希望它是通用的,你必须使用反射来循环遍历成员并动态构建查询 xml。 类似:

Type type = TypeOf(Contact);
PropertyInfo[] properties = type.GetProperties();

foreach (PropertyInfo property in properties)
{
    /////here you chain the members for the xml
    Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(obj, null));
} 

【讨论】:

    【解决方案2】:

    要从任何实体获取详细信息,请使用以下方法。 在使用这个通用的 Fetchxml 生成器之前,请阅读注意和参数说明。

    Note : fieldToQuery, operatorForCondition, fieldQueryValue  Should have Array Same count to work this function  and well Mappep with respective to each other to get desired result  
    
    parameter name = "entityName" = Name of Entity of which details to fetch.  
    parameter name = "fieldsToSearch" = What all fields you want to fetch, if Sent Null, by default all fields are fetched from entity  
    parameter name = "filterType" = "AND" or "OR"   
    parameter name = "fieldToQuery" = Array of  Field name on which to query  
    parameter name = "operatorForCondition" = Array of operator between fieldToQuery(Field Name) and fieldQueryValue(Field Value) like "eq, like"  
    parameter name = "fieldQueryValue" = Array of Field Value respective to fieldToQuery (Field Name) by which to query  
    

    此处的功能统计信息
    `
    公共静态 Microsoft.Xrm.Sdk.EntityCollection RetrieveEntityDetailsByFetchXml(字符串 entityName, string[] fieldsToSearch, string filterType, string[] fieldToQuery, string[] operatorForCondition, string[] fieldQueryValue) {

            string _fetchDetailsXml = @"<fetch version='1.0' mapping='logical' output-format='xml-platform'> <entity name='" + entityName + "'> ";
    
            _fetchDetailsXml = GenerateFetchXMLForAttributes(fieldsToSearch, _fetchDetailsXml);
    
            _fetchDetailsXml += "<filter type='" + filterType + "'>";
    
            if (fieldQueryValue.Count() != fieldToQuery.Count() && fieldToQuery.Count() != operatorForCondition.Count())
            {
                throw new ApplicationException("FieldtoQuery and FieldQueryValue are not mapped correctly fieldToQuery : " + fieldToQuery.Count() + " fieldQueryValue : " + fieldQueryValue.Count() + ".");
            }
    
            _fetchDetailsXml = GenerateFetchXMLForConditions(fieldToQuery, operatorForCondition, fieldQueryValue, _fetchDetailsXml);
    
            _fetchDetailsXml += "</filter> </entity> </fetch>";
    
    
            Microsoft.Xrm.Sdk.EntityCollection _entityDetailsColl = CrmConnectionsManager.OrganizationServiceProxy.RetrieveMultiple(new FetchExpression(_fetchDetailsXml));      
            // Note : "_entityDetailsColl" cast this object to respective Entity object 
    
            if (_entityDetailsColl != null && _entityDetailsColl.Entities.Count() > 0)
            {
                return _entityDetailsColl;
            }
    
            return null;
        }  `
    

    `

        public static string GenerateFetchXMLForAttributes(string[] fieldsToSearch, string fetchXml)
        {
            if (fieldsToSearch != null && fieldsToSearch.Count() > 0)
            {
                foreach (string field in fieldsToSearch)
                {
                    fetchXml += "<attribute name='" + field + "' />";
                }
            }
            else
            {
                fetchXml += "<all-attributes/>";
            }
    
            return fetchXml;
        }
    
        public static string GenerateFetchXMLForConditions(string[]fieldToQuery,string[] operatorForCondition, string[] fieldQueryValue, string _fetchDetailsXml)
        {
            if (fieldToQuery != null && fieldToQuery.Count() > 0)
            {
                for (int count = 0; count < fieldToQuery.Count(); count++)
                {
                    _fetchDetailsXml += "<condition attribute='" + fieldToQuery[count] + "'  operator='" + operatorForCondition[count] + "' value='" + fieldQueryValue[count] + "' uiname='' uitype='' /> ";
                }
            }
            else
            {
                _fetchDetailsXml += "<all-attributes/>";
            }
    
            return _fetchDetailsXml;
        }
    

    `

    【讨论】:

      【解决方案3】:

      检索“所有属性”在计算上比仅检索您需要的属性更昂贵(还要考虑 IO 成本)。如果您正在寻找一种查看属性的方法,请为您需要的人编写代码,试试这个:

      在 CRM 网络 GUI 中:

      1.导航到显示相关记录的视图 2.单击功能区栏中的高级查找按钮 3.配置您的“查找”,直到它显示您正在查找的记录 4.单击功能区栏中的下载获取 XML 按钮 5.使用文本查看器(或您喜欢的开发工具)打开文件

      如果你想直接使用这个 XML,你可以考虑:

      使用 FetchXML 构造查询
      http://msdn.microsoft.com/en-us/library/gg328117.aspx

      【讨论】:

        猜你喜欢
        • 2011-11-24
        • 2011-09-25
        • 2011-08-18
        • 1970-01-01
        • 1970-01-01
        • 2011-12-16
        • 2015-03-13
        • 1970-01-01
        • 2013-05-19
        相关资源
        最近更新 更多