Create early bound entity classes with the code generation tool (CrmSvcUtil.exe)

CrmSvcUtil.exe is a command-line code generation tool for use with Dynamics 365 for Customer Engagement. This tool generates early-bound .NET Framework classes that represent the entity data model used by Dynamics 365 for Customer Engagement.

The code generation tool (CrmSvcUtil.exe) is distributed as part of the Microsoft.CrmSdk.CoreTools NuGet package. For information about downloading the code generation tool (CrmSvcUtil.exe), see Download tools from NuGet.

About the code generation tool

The CrmSvcUtil.exe tool creates a Microsoft Visual C# or Visual Basic .NET output file that contains strongly-typed classes for entities in your organization. This includes custom entities and attributes. This output file contains one class for each entity, providing early binding and IntelliSense support in Visual Studio to aid you as you write custom code. The generated classes are partial classes that can be extended with custom business logic in separate files. You can also create extensions to this tool. For more information, see Create Extensions for the Code Generation Tool.

The tool can also be used to generate a class derived from the OrganizationServiceContext class that acts as an entity container in the entity data model. This service context provides the facilities for tracking changes and managing identities, concurrency, and relationships. This class also exposes a SaveChanges() method that writes inserts, updates, and deletes records in Dynamics 365 for Customer Engagement. For more information, see Use OrganizatioServiceContext in the Common Data Service documentation.

The code generation tool takes several parameters that determine the contents of the file that is created. The parameters can be passed in from the command line when you run the tool or in a .NET-connected application configuration file.

The classes created by the code generation tool are designed to be built into a class library that can be referenced by projects that use Dynamics 365 for Customer Engagement. After you have generated the class file using the tool, you should add the file to your Visual Studio project. You must also add references to several assemblies that the generated classes are dependent upon.

The following lists assemblies that must be referenced in your project when you use the generated code file.

  • Microsoft.Crm.Sdk.Proxy.dll
  • Microsoft.Xrm.Sdk.dll

You can find these assemblies in the folder where you download the tools. Folder path: <Download_directory>\tools\CoreTools. For example, if you download the tools in devtools folder on your D drive, you can find the assemblies in D:\devtools\Tools\CoreTools.

More information about using the tool is available under Generate early-bound classes for the Organization service.

Run the code generation tool

Run the CrmSvcUtil.exe tool from the SDK\Bin folder. If you run the tool from another folder location, make sure that a copy of the Microsoft.Xrm.Sdk.dll assembly is in that same folder.

The following sample shows the format for running the tool from the command line for an on-premises installation of Dynamics 365 for Customer Engagement. You supply the parameter values for your installation.

CrmSvcUtil.exe /url:http://<serverName>/<organizationName>/XRMServices/2011/Organization.svc    /out:<outputFilename>.cs /username:<username> /password:<password> /domain:<domainName>    /namespace:<outputNamespace> /serviceContextName:<serviceContextName>  

You can look up the correct URL in the web application by selecting Settings, navigating to Customizations, and then choosing Developer Resources. The URL is shown under Organization Service.

To list the supported command-line parameters, use the following command.

CrmSvcUtil.exe /?  

Usage examples

The following examples show how to use of the code generation tool from the command line. Note that user name and password are optional parameters. If your credentials for the target Dynamics 365 for Customer Engagement server are stored in the Windows credential vault, you do not have to provide them to run the code generation tool.

Claims authentication – Active Directory

The following sample shows how to run the code generation tool by using claims authentication in Active Directory. Note the use of https because this sample server is using Transport Layer Security (TLS) or Secure Sockets Layer (SSL).

CrmSvcUtil.exe /url:https://myport:555/MyOrg/XRMServices/2011/Organization.svc /out:GeneratedCode.cs /username:administrator /password:password

Claims authentication - IFD

The following sample shows how to run the code generation tool using claims authentication.

CrmSvcUtil.exe /url: /out:GeneratedCode.cs /username:administrator /password:p@ssword!

Use the configuration File

The CrmSvcUtil.exe.config configuration file must be in the same folder as the CrmSvcUtil.exe tool. The configuration file uses the standard key/value pairs in the appSettings section. However, if you enter a value at the command line, that value will be used instead of the one in the configuration file. Any key/value pairs found in the application configuration file that do not match any of the expected parameters are ignored.

Do not include the url and namespace parameters in the configuration file. These must be entered from the command line when the CrmSvcUtil.exe tool is being run.

The following sample shows how to configure the output file and the domain name parameters in the application configuration file using shortcut keys.

    <add key="o" value="CrmProxy.cs"/>    
    <add key="d" value="mydomain"/>

See Also

Developer Tools for Dynamics 365 Customer Engagement (on-premises)
Browse the Metadata for Your Organization
Create an Extensions for the Code Generation Tool
Late-bound and Early-bound programming using the Organization service
Troubleshooting Tips
Run a simple program using Customer Engagement web services