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
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:
- /url: your organization name
- /out: output file Name
- /username: is used to sign in to your Dataverse environment.
- /password: is used to sign in to your Dataverse environment.
- /namespace: The namespace for the generated code.
- /serviceContextName: The name of the generated organization service context class. If no value is supplied, no service context is created.
- /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:
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.
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".
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
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
-
Generate Earlybound entities in Dynamics 365 CRMOn compile errors, remember to create the 'Global OptionSets'.Add parameter /generateGlobalOptionSets ^
-
[mention:58374493e0b94ceea45a17f1ee7968b9:e9ed411860ed4f2ba0265705b8793d05] Thank you for your updates, I will add all the Parameters to the End. Thank you.
-
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
*This post is locked for comments