Monday, December 20, 2010

Two ways to build a CRM QueryExpression

I always wondered what FetchXML was useful for and I’m starting to see its value.  I ran into a wall with the new Linq based Advanced Developer Extensions and have reverted back to the “stone-age” web services.  Having to build QueryExpressions by hand led me to the discovery that I could actually use FetchXML syntax to build my expressions by using a FetchXmlToQueryExpressionRequest Combining this with Stunnware’s FetchXML builder might be even faster once you get the hang of it.  Certainly makes debugging a complex join query snappy.

 

Check out these two ways to get the exact same dataset:

 

        private static void FetchAppsToProcess(Microsoft.Crm.SdkTypeProxy.CrmService crmWebSvc)

        {

            string fxml =

                @"<fetch mapping='logical' count='50' version='1.0'>" +

                " <entity name='new_transistionapplication'>" +

                "       <attribute name='new_transistionapplicationid' />" +

                "       <filter>" +

                "             <condition attribute='createdon' operator='lt' value='9/17/2010' />" +

                "             <condition attribute='new_lastname' operator='eq' value='Smith' />" +

                "       </filter>" +

                " </entity>" +

                "</fetch>";

 

            //string xmlResult = crmWebSvc.Fetch(fxml);

            FetchXmlToQueryExpressionRequest fxmlReq =

             new FetchXmlToQueryExpressionRequest() { FetchXml = fxml };

            FetchXmlToQueryExpressionResponse expression =

             (FetchXmlToQueryExpressionResponse)crmWebSvc.Execute(fxmlReq);

 

            var request = new RetrieveMultipleRequest { Query = expression.Query, ReturnDynamicEntities = true };

            var response = (RetrieveMultipleResponse)crmWebSvc.Execute(request);

 

        }

 

        private static void GetAppsToProcess(Microsoft.Crm.SdkTypeProxy.CrmService crmWebSvc)

        {

            List<string> reqBoards = new List<string>();

 

            var con = new ConditionExpression

            {

                AttributeName = "createdon",

                Operator = ConditionOperator.LessThan,

                Values = new object[] { DateTime.Parse("9/17/2010") }

            };

 

            var conName = new ConditionExpression

            {

                AttributeName = "new_lastname",

                Operator = ConditionOperator.Equal,

                Values = new[] { "Smith" }

            };

 

            var filter = new FilterExpression();

           

            filter.AddCondition(con);

            filter.AddCondition(conName);

            filter.FilterOperator = LogicalOperator.And;

           

 

            // The Entity returning the results

            var expression = new QueryExpression

            {

                EntityName = "new_transistionapplication",

                Criteria = filter

            };

           

            expression.ColumnSet = new ColumnSet(new string[] { "new_transistionapplicationid", "new_requestedboardappointments" });

 

            var request = new RetrieveMultipleRequest { Query = expression, ReturnDynamicEntities = true };

            var response = (RetrieveMultipleResponse)crmWebSvc.Execute(request);

        }

No comments: