Search code examples
c#dynamics-crmcustomcolumn

C# CRM SDK Custom columns


How do I build a CRM SDK QueryExpression where the values of two columns are combined to a new one? In MySQL, my query would look like that:

SELECT *, (`latest_maintenance`+`maintenance_interval`) as `next_maintenance` FROM `servers` ORDER BY `next_maintenance` DESC

In C#, however, I have only managed to do the following:

var retrieveRequest = new RetrieveMultipleRequest();

retrieveRequest.Query = new QueryExpression
                                    {
                                        EntityName = "server",
                                        ColumnSet = new ColumnSet(new[] {"latest_maintenance", "maintenance_interval"})
                                    };

var crmReponse = (RetrieveMultipleResponse) service.Execute(retrieveRequest);

How would I join "latest_maintenance" and "maintenance_interval" to "next_maintenace" in order to be able to use an OrderExpression?

EDIT: How would I make a simple string-based query for Microsoft Dynamics CRM? Seems like a way easier and more intelligible way than their default one.


Solution

  • If you are using the SDK for server-side code, then your choice is QueryExpression or FetchXml. Neither will do what you are asking, natively.

    Remember that with QueryExpression you are returning typed objects, not just strings so if you are to concatenate two values, they need a container in which to be stored, i.e. an attribute.

    Can you not just "order by" column1 then column2? I.e.

    var myQueryExpression = new QueryExpression
                                    {
                                        EntityName = "server",
                                        ColumnSet = new ColumnSet(new[]{
                                            "latest_maintenance", 
                                            "maintenance_interval"})
                                    };
    myQueryExpression.AddOrder("latest_maintenance", OrderType.Ascending);
    myQueryExpression.AddOrder("maintenance_interval", OrderType.Ascending);