Fetchxml MS CRM 2011获取XML

Fetchxml MS CRM 2011获取XML,fetchxml,Fetchxml,Generic Fetch XML以从Microsoft Dynamics CRM 2011中的任何实体获取所有属性?如果希望它是泛型的,则必须使用反射来循环成员并动态生成查询XML。 比如: Type type = TypeOf(Contact); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { /////here you chain t

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

如果希望它是泛型的,则必须使用反射来循环成员并动态生成查询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));
} 

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

此处的函数统计信息
`
公共静态Microsoft.Xrm.Sdk.EntityCollection RetrieveEntityDetailsByFetchXml(字符串entityName,字符串[]字段搜索,字符串筛选类型,字符串[]字段查询,字符串[]运算符ForCondition,字符串[]字段查询值) {

        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;
    }  `
string_fetchDetailsXml=@”;
_fetchDetailsXml=GenerateFetchXMLForAttributes(fieldsToSearch,_fetchDetailsXml);
_fetchDetailsXml+=“”;
if(fieldQueryValue.Count()!=fieldToQuery.Count()&&fieldToQuery.Count()!=operatorForCondition.Count())
{
抛出新的ApplicationException(“FieldtoQuery和FieldQueryValue未正确映射FieldtoQuery:+FieldtoQuery.Count()+”FieldQueryValue:+FieldQueryValue.Count()+”);
}
_fetchDetailsXml=GenerateFetchXMLForConditions(fieldToQuery,operatorForCondition,fieldQueryValue,_fetchDetailsXml);
_fetchDetailsXml+=“”;
Microsoft.Xrm.Sdk.EntityCollection _entityDetailsColl=CrmConnectionsManager.OrganizationServiceProxy.RetrieveMultiple(新的FetchExpression(_fetchDetailsXml));
//注意:“\u entityDetailsColl”将此对象强制转换为相应的实体对象
if(_entityDetailsColl!=null&&u entityDetailsColl.Entities.Count()>0)
{
返回_entityDetailsColl;
}
返回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;
    }
公共静态字符串GenerateFetchXmlForAttribute(string[]fieldsToSearch,string fetchXml)
{
if(fieldsToSearch!=null&&fieldsToSearch.Count()>0)
{
foreach(fieldsToSearch中的字符串字段)
{
fetchXml+=“”;
}
}
其他的
{
fetchXml+=“”;
}
返回fetchXml;
}
公共静态字符串GenerateFetchXMLForConditions(字符串[]fieldToQuery,字符串[]operatorForCondition,字符串[]fieldQueryValue,字符串_fetchDetailsXml)
{
if(fieldToQuery!=null&&fieldToQuery.Count()>0)
{
对于(int count=0;count

< P>检索“所有属性”在计算上比检索所需的更昂贵(也考虑IO成本)。如果您正在寻找查看属性的方法,请为您需要的属性编写代码,尝试以下操作:

在CRM web GUI中:

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

如果要直接使用此XML,可以考虑:

使用FetchXML构造查询

很抱歉,问题不清楚。现在我更新了问题。请参考我的答案以寻求解决方案。
    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;
    }