Late Binding in Dynamics CRM 2011

Roshan Mehta, 13 May 2012

I have previously taken you through some simple examples of early binding in Microsoft Dynamics CRM 2011. Late binding involves the use of the “Entity” class which can manage entities of any type. For example, we can use the “Entity” object to programmatically create Accounts, Leads, or Cases. In this post, we will take a look at some late binding examples using methods available in the Microsoft Dynamics CRM SDK.

Type checking is performed at runtime so developers using the late binding methods must explicitly cast attribute values to the appropriate types otherwise custom code will fail during execution. Unlike early binding, developers need to be familiar with the schema names of entities and their attributes as intellisense is not available using this method.

Example – Creating Records

The example below shows how to create an Account called “Frosty’s Ice Cream Store” and set a custom attribute called “Business Hours”. All we need to do is specify the schema name of the Account entity as well as the schema names for the attributes we want to set.

 Late Binding in Dynamics CRM 2011

If we tried to set the “name” attribute to an integer, the code would still compile. However, when our custom application runs, we will encounter an error.

Example – Retrieving  Records

The following example shows how to use the Retrieve method to retrieve an Account. It is very similar to early binding except we are not assigning the result to a specific type. Instead, we assign the result to the “Entity” type.

 Late Binding in Dynamics CRM 2011

Example – Updating Records

The following example shows how to update the telephone2  field for an Account.

Late Binding in Dynamics CRM 2011