Wednesday, October 02, 2013

Code snippet to convert fetch xml to query expression crm 2011


Use the code template from here

Code:
string fetchExp = @"<fetch version=""1.0"" output-format=""xml-platform"" mapping=""logical"" distinct=""false"">
                                      <entity name=""task"">
                                        <attribute name=""activityid"" />
                                        <attribute name=""ownerid"" />
                                        <order attribute=""subject"" descending=""false"" />
                                        <filter type=""and"">
                                          <condition attribute=""ssb_type"" operator=""eq"" value=""867700004"" />
                                          <condition attribute=""statuscode"" operator=""in"">
                                            <value>2</value>
                                            <value>3</value>
                                            <value>4</value>
                                            <value>7</value>
                                          </condition>
                                          <condition attribute=""description"" operator=""like"" value=""%MÃ¥nedlig risikovurdering%"" />
                                          <condition attribute=""createdon"" operator=""last-x-days"" value=""365"" />
                                        </filter>
                                        <link-entity name=""account"" from=""accountid"" to=""regardingobjectid"" alias=""accountOwner"">
                                          <attribute name=""efs_kundeansvarligid"" />
                                          <filter type=""and"">
                                            <condition attribute=""efs_kundeansvarligid"" operator=""not-null"" />
                                          </filter>
                                        </link-entity>
                                      </entity>
                                    </fetch>";
                FetchExpression fe = new FetchExpression(fetchExp);

                FetchXmlToQueryExpressionRequest req = new FetchXmlToQueryExpressionRequest();
                req.FetchXml = fetchExp;
                FetchXmlToQueryExpressionResponse resp = (FetchXmlToQueryExpressionResponse)_service.Execute(req);

                QueryExpression con_qe = resp.Query;
                EntityCollection con_coll = _service.RetrieveMultiple(con_qe);

No comments: