Skip to main content

Notifications

Generate Earlybound entities in Dynamics 365 CRM

Introduction

Early-bound entities in Dynamics 365 are generated classes that provide compile-time validation and type-checking for entities in your code. This can help prevent runtime errors and make your code more efficient.

In this post, we'll walk you through the steps to generate early-bound entities for your Dynamics 365 instance.

Prerequisite:

Microsoft.CrmSdk.CoreTools has been installed on your local machine.

Regarding how to install Microsoft.CrmSdk.CoreTools, you can follow this link.


Step 1: To generate early bound entities, we need to use the CrmSvcUtil.exe tool, which is included with Microsoft.CrmSdk.CoreTools.

CrmSvcUtil is a command-line code generation tool for use with Microsoft Dataverse. The tool generates early-bound .NET Framework classes that represent the Entity Data Model (EDM) used by Dataverse. 

Step 2: Navigate to the folder where CrmSvcUtil.exe was located.

Step 3: Open PowerShell here

pastedimage1686398017539v1.png

Step 4: Run the CrmSvcUtil.exe application from the folder where it is installed.
The code generation tool takes several parameters that determine the contents of the file that is created.

    
    CrmSvcUtil.exe ^
/url:https://.api.crm.dynamics.com/XRMServices/2011/Organization.svc ^
/out:..\D365.Comm.ConsoleApp\EntitiWrapper\Earlybound.cs ^
/username: ^
/password: ^
/namespace:D365.Comm.ConsoleApp.EntitiWrapper ^
/serviceContextName:ServiceContext ^
/entitynamesfilter:account;contact

Parameters:

  1. /url: your organization name
  2. /out: output file Name
  3. /username: is used to sign in to your Dataverse environment.
  4. /password: is used to sign in to your Dataverse environment.
  5. /namespace: The namespace for the generated code. 
  6. /serviceContextName: The name of the generated organization service context class. If no value is supplied, no service context is created.
  7. /entitynamesfilter:Filters the list of entities retrieved when reading data from Dataverse. Passed in as a semicolon separated list using the form <entitylogicalname>;<entitylogicalname>;...

    The examples above use the carat (^) character to break up the list of parameters for readability. You can compose the command parameters with arguments using Notepad and then paste it into the command line.

Step 5:Run your code via a batch (.bat) file

As a best practice, you can specify the parameters you want to pass directly in the command line or via a batch (.bat) file you can run to generate new classes.

Next is an example:

pastedimage1686400758022v2.png

pastedimage1686402191206v5.png

Step 6: Double-click the .bat file to generate the early bound files

 The tool will connect to your Dynamics 365 instance, generate the early bound entity classes, and save them in the specified file location.

pastedimage1686402100543v4.png

Step 7: Add the coretools folder to your Visual Studio project.
Now that you have the coretools and .bat file you can add them to your Visual Studio project. To do this, right-click on your project in the Solution Explorer, select "Add", and then "Existing Item". Navigate to the location where you saved the tools, select them, and click "Add".

pastedimage1686402938302v7.png

Step 8: Use the generated classes in your code.
Once you've added the generated files to your project, you can use the early bound entity classes in your code.

For example

pastedimage1686402572754v6.png

In Conclusion:

And that's it! With these steps, you can generate early bound entity classes for your Dynamics 365 instance and use them in your code.

The END

Comments

*This post is locked for comments

  • ctreudler (Flow7 GmbH) Profile Picture ctreudler (Flow7 GmbH) 45
    Posted at
    Generate Earlybound entities in Dynamics 365 CRM
    On compile errors, remember to create the 'Global OptionSets'.
    Add parameter /generateGlobalOptionSets ^
  • Guowei_Xu Profile Picture Guowei_Xu 407
    Posted at
    [mention:58374493e0b94ceea45a17f1ee7968b9:e9ed411860ed4f2ba0265705b8793d05] Thank you for your updates, I will add all the Parameters to the End. Thank you.
  • Flydancer Profile Picture Flydancer 1,332
    Posted at
    In case you want to split the files by entity instead of having them all in one file, use the parameters /splitfiles in combination with /outdir