Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011

Sunday, August 18, 2013

Step-By-Step How-to programmatically CREATE new entity items with late binding


by Carmel Schvartzman
  1. In this walkthrough we will develop an application to programmatically CREATE instances of an entity . 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:
    1
  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:3
  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 CREATE a new entity object:5
    IMPORTANT: when using the LATE BINDING approach, we need to use LOGIC entity names, not SCHEMA names(p.e. "account" but no "Account").
  7. Finally, we have to SAVE the new item, using the "Create(|entity|)" method of the Organization service, which creates the entity instance :6
    As you can see, this method returns the GUID of the newly created entity object. You can use it in the code to update attributes. . You can even state to the CRM engine which GUID do you want to use, specifying it when calling the Create() method, and CRM  will use that guid for the new object.
 That's all
Happy programming    :-)

כרמל שוורצמן

No comments:

Post a Comment