Create early bound entity classes with the code generation tool (CrmSvcUtil.exe)
Applies to Dynamics 365 for Customer Engagement apps version 9.x
CrmSvcUtil.exe is a command-line code generation tool for use with Dynamics 365 for Customer Engagement apps. This tool generates early-bound .NET Framework classes that represent the entity data model used by Dynamics 365 for Customer Engagement apps.
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 apps. For more information, see Use the Organization Service Context Class.
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 apps. 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.
You can find these assemblies in the folder where you download the tools.
For example, if you download the tools in devtools folder on your D drive, you can find the assemblies in
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 apps. 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.
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 apps 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:https://myorg.crm.com:555/XRMServices/2011/Organization.svc /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
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.
<appSettings> <add key="o" value="CrmProxy.cs"/> <add key="d" value="mydomain"/> </appSettings>
Developer Tools for Dynamics 365 for Customer Engagement apps
Browse the Metadata for Your Organization
Create an Extensions for the Code Generation Tool
Use the Early Bound Entity Classes for Create, Update and Delete
Run a simple program using Customer Engagement web services