Вопрос

Generic Fetch XML To Get All attributes from any entity in Microsoft Dynamics CRM 2011?

Это было полезно?

Решение 2

To fetch details from any entity use below method. Please read Note and Parameter Description before using this generic Fetchxml generator.

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  

Function stats from here
`
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;
    }

`

Другие советы

if you want it to be generic, youll have to use reflection to loop through the members and build the query xml dynamically. something like:

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));
} 

Retrieving “all attributes” is computationally more expensive than retrieving just those you need (also consider the IO cost). If you are looking for a way to view the attributes then write code for just those you need try this:

In the CRM web GUI:

1.Navigate to the view that displays the records in question 2.Click the Advanced Find button in the Ribbon Bar 3.Configure your “find” until it shows the records you are looking for 4.Click the Download Fetch XML button in the Ribbon Bar 5.Open the file with a text viewer (or your favorite development tool)

If you want to use this XML directly you might consider:

Use FetchXML to Construct a Query
http://msdn.microsoft.com/en-us/library/gg328117.aspx

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top