Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle the fetchxml result data?

I have avoided working with fetchxml as I have been unsure the best way to handle the result data after calling crmService.Fetch(fetchXml). In a couple of situations, I have used an XDocument with LINQ to retrieve the data from this data structure, such as:

XDocument resultset = XDocument.Parse(_service.Fetch(fetchXml));
if (resultset.Root == null || !resultset.Root.Elements("result").Any())
{
    return;
}
foreach (var displayItem in resultset.Root.Elements("result").Select(item => item.Element(displayAttributeName)).Distinct())
{
    if (displayItem!= null && displayItem.Value != null)
    {
        dropDownList.Items.Add(displayItem.Value);    
    }
}

What is the best way to handle fetchxml result data, so that it can be easily used. Applications such as passing these records into an ASP.NET datagrid would be quite useful.

like image 343
Luke Baulch Avatar asked Jul 31 '09 00:07

Luke Baulch


People also ask

How do you count in FetchXML?

Select the entity, contact, in this case, paste the FetchXML query and click Execute Count. We will get the count.

What does FetchXML return?

FetchXML query results When you execute a FetchXML query by using the OrganizationServiceProxy. RetrieveMultiple(QueryBase) method, the return value is an EntityCollection that contains the results of the query. You can then iterate through the table collection.

How do I use FetchXML group by?

FetchXML includes grouping and aggregation features that you can use this function calculate sum, average min, max and count. For create an aggregate attribute, set the keyword aggregate to true, then specify entity name, attribute name and mandatory to alias(variable name).


3 Answers

I enjoy the flexibility of FetchXML and so I developed the following function that returns a datatable for use in binding to grids and repeaters and so forth.

        /// <summary>
    /// Takes a CRM FetchXML query and returns a DataTable
    /// </summary>
    /// <param name="fetchXml">The FetchXML query</param>
    /// <param name="requiredFields">A array of columns you'd expect returned. This is required as if there is no data for a field/column CRM will not return it which could impact databinding</param>
    /// <returns>A datatable containing the results of the FetchXML</returns>
    public static DataTable FetchXML2DataTable(string fetchXml, string[] requiredFields)
    {
        CrmService tomService = new CrmService();
        tomService = CrmWebService;

        string result = tomService.Fetch(fetchXml);
        DataSet ds = new DataSet();

        System.IO.StringReader reader = new System.IO.StringReader(result);
        ds.ReadXml(reader);

        DataTable dt = ds.Tables[1];

        //check all required columns are present otherwise add them to make life easier for databinding at the top level
        //caused by CRM not returning fields if they contain no data
        foreach (string field in requiredFields)
        {   //Check for column names and nested tables
            if ((dt.Columns.IndexOf(field) < 0) && (dt.DataSet.Tables.IndexOf(field) <0))
            {                    
                //Add column to datatable even though it is empty for reason stated above
                dt.Columns.Add(new DataColumn(field));
            }
        }            

        return dt;
    }

The requiredFields string array is there because columns aren't returned if your result set contains no data with that column, however I might want the column in place for the exact reason of binding to datagrids etc.

CrmService is a singleton class that initates the webservice.

Hopefully this is of use to you.

like image 93
Fishcake Avatar answered Oct 10 '22 12:10

Fishcake


I typically avoid FetchXML for this very reason. You can use the RetrieveMultiple to get strongly typed BusinessEntity objects and basically do the same stuff.

But if you want to use the FetchXML this sample should cover you:

http://msdn.microsoft.com/en-us/library/ms914457.aspx

like image 39
brendan Avatar answered Oct 10 '22 14:10

brendan


With QueryExpression you can't query many-to-many entity and can't retrieve attributes from more than one entity at once, so you must use FetchXML.

Unfortunately LinqToCRM's codeplex project has been turned obsolete (1year without new release, but it seems to be a good implementation, better than the microsoft's release) with the release of 4.0.12 of CRM's SDK that contained a linq provider for dynamics crm, but I read some article about this new release and its not very good, seems to be a "poor implementation" with lot of limitations (forced cache etc.).

I see lot of people using LinqToXML and DataSet for leading with FetchXML result, but I could not say what the best way to deal with it. What do you think about this?

Christophe Trevisani Chavey.

like image 22
Christophe Trevisani Chavey Avatar answered Oct 10 '22 13:10

Christophe Trevisani Chavey