Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011

Monday, September 2, 2013

How to programmatically retrieve entities in Dynamics CRM


by Carmel Schvartzman

  1. In this Step-By-Step walkthrough we will develop an application to programmatically retrieve entities in Dynamics CRM, using a QUERY EXPRESSION . There are two ways in CRM 2011 to do so.You can choose between an LATE-BINDING and an EARLY-BINDING approach: in early binding we have compile-time checking of all types, but when coding late binding we get runtime checking, that means there is a check only when an object is created or when an action is performed on it.
    To create all the types and entities existing  in our CRM organization, CRM have a tool called CrmSvcUtil.exe, a code generation tool. We use it if we want to create EARLY-BOUND entity classes. The LATE-BOUND approach uses the Organization Service webservice of the CRM, which includes both data and metadata attributes.  In this walkthrough we'll work with late-bound entities. To do so we'll develop a new project and use the OrganizationService in order to create new objects.
  2. First, start VISUAL STUDIO 2010 and create a console project:
    programmatically retrieve entities in Dynamics CRM
  3. To use the OrganizationService, we need to add references to the following assemblies:2(You'll find them in the Bin folder of the CRM 2011 SDK and in .NET tab of the ADD REFERENCES)
  4. Now change the target framework to .NET Framework 4:1programmatically retrieve entities in Dynamics CRM
  5. Next, we have to set up the webservice, using the Uri of the CRM organization webservice (its format is http://YOUR_CRM_SERVER/YOUR_CRM_ORGANIZATION/XrmServices/2011/Organization.svc):4
  6. After we connect to our Organization webservice, we want to RETRIEVE an entity object. To do so we have three options:
    1) create a QUERY EXPRESSION
    2) use LINQ
    3) use the FETCH XML query language

    In this walkthrough we'll learn the first option.

    IMPORTANT: when using the LATE BINDING approach, we need to use LOGIC entity names, not SCHEMA names(p.e. "account" but no "Account").
  7. We'll create a QUERY EXPRESSION, which works according to a FILTER that uses some pre-defined CONDITION. So first let's create that CONDITION. A condition includes an expression and its operators.
  8. Let's say for the sake of the example, that we want to retrieve all the accounts of customers called "David". If so, we need an SQL where-clause like this: "where name like '%David%'. Therefore we'll use the "LIKE" operator:
  9. Next we create the CONDITION expression: NAME + "LIKE" + "%David%":
  10. Now we can create the FILTER to get the required records: we just create a FilterExpression instance and set its Condition property to our condition previously stated:


   11. Next we can explicit which fields of the entity we want to get, using an ColumnSet object:

      

    12. Finally we call the RetrieveMultiple() method of the organization service, using our QueryExpression as a parameter:
        

13. And the method returns an EntityCollection containing all the records we requested.

That's all
The present tutorial is about how to programmatically retrieve entities in Dynamics CRM.
Happy programming    :-)

כרמל שוורצמן

No comments:

Post a Comment