-2

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

4

3 回答 3

0

如果您希望它是通用的,则必须使用反射来循环遍历成员并动态构建查询 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));
} 
于 2013-10-30T19:30:38.743 回答
0

要从任何实体获取详细信息,请使用以下方法。在使用这个通用的 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  

此处的函数统计信息
`
public static Microsoft.Xrm.Sdk.EntityCollection RetrieveEntityDetailsByFetchXml(string 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;
    }

`

于 2013-10-31T07:00:30.570 回答
0

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

在 CRM Web GUI 中:

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

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

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

于 2014-02-13T16:51:27.010 回答