Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011

Tuesday, September 10, 2013

How to retrieve entities using FetchXml in Dynamics CRM

by Carmel Schvartzman

  1. In this walkthrough we will learn Step-By-Step How-to develop an application to programmatically RETRIEVE instances of an entity, using a FetchXml query expression, in Dynamics CRM.
  2. FetchXml is a query language used by Microsoft Dynamics CRM 2011, that can be serialized and saved for future uses. It also allows users to create custom reports. The FetchXml query can be used to instantiate a FetchExpression for use by the RetrieveMultiple() method of the IOrganizationService.
  3. There is no need to create an xml file or code: Microsoft Dynamics CRM 2011 can create automatically a FetchXml for us, while using the Advanced Search. Therefore, we'll use this method to create the query.
  4. First, we create a new console project an Visual Studio:
    How to retrieve entities using FetchXml in Dynamics CRM
  5. Add the following references to the project:
  6. At the Properties tab, change the target framework to .NET 4:
    retrieve entities using FetchXml in Dynamics CRM
  7. Next, write the standard CRM SDK code to instantiate the IOrganizationService:

  8. Now, turn to your CRM UI. Let's say we want to retrieve all the Accounts named "Sean":go to the Accounts workplace, and select "ADVANCED SEARCH":

  9. Select NEW view:
  10. On the FIELDS combo, select the field Account Name:
  11. Select the "Contains" option:
  12. Finally, our condition will be : "Account Name contains 'Sean'":
  13. Press RESULTS to see whether the query returns what we need:


    The results are correct. But let's say we don't need the "Primary contact" column, so we delete it:
  14. Select "Edit Columns" to remove the field:
  15. Select the column and click on "Remove":
  16. Now we want of course to add another column: we'll learn how to add a column that relates to another entity: click "Add Columns":
  17. This Campaign Name field holds the dependency N:1 between Account to Campaign. Add the column:
  18. Go to "Advanced Find" and press RESULTS again to see what we get:


    You can see we got a link that leads to the related entity Campaign. That's good.
  19. Now that we got what we wanted, let's download the query that did the search:
  20. Save the downloaded file and take a look to the xml: you'll recognize the search condition that we stated:
  21. Copy-paste the xml to a string in our project:
  22. Now replace the ( " ) with a ( ' ) :
  23. Next, instantiate a FetchExpression with the query string:

    How to retrieve entities using FetchXml in Dynamics CRM
  24. And finally, call the IOrganizationService method RetrieveMultiple(), using the expression:



    Now we get the required records into an EntityCollection, that we can loop with a foreach:



    This is about How to retrieve entities using FetchXml in Dynamics CRM.
  25. That's all..
    Happy programming!!!!

כתב: כרמל שוורצמן

No comments:

Post a Comment