by Carmel Schvartzman
- 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. - First, start VISUAL STUDIO 2010 and create a console project:
- To use the OrganizationService, we need to add references to the following assemblies:(You'll find them in the Bin folder of the CRM 2011 SDK and in .NET tab of the ADD REFERENCES)
- Now change the target framework to .NET Framework 4:
- 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):
- 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"). - 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.
- 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:
- Next we create the CONDITION expression: NAME + "LIKE" + "%David%":
- 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