Create early bound entity classes with the code generation tool (CrmSvcUtil.exe)
Applies To: CRM 2015 on-prem, CRM Online
CrmSvcUtil.exe is a command-line code generation tool for use with Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online 2015 Update. This tool generates early-bound .NET Framework classes that represent the entity data model used by Microsoft Dynamics CRM. This tool is in the SDK package in the
SDK\Bin folder. Download the Microsoft Dynamics CRM SDK package.
About the code generation tool
The CrmSvcUtil.exe tool creates a Microsoft Visual C# or Microsoft 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 Microsoft 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 Microsoft Dynamics CRM. For more information, see Use the OrganizationServiceContext 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 Microsoft Dynamics CRM. 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.
These assemblies can be found in the The SDK\Bin folder of the SDK. Download the Microsoft Dynamics CRM SDK package.
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 Microsoft Dynamics CRM. 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>
The following sample shows the format for running the tool from the command line with Microsoft Dynamics CRM Online. You supply the parameter values appropriate for your account and server.
CrmSvcUtil.exe /url:https://<organizationUrlName>.api.crm.dynamics.com/XRMServices/2011/Organization.svc /out:<outputFilename>.cs /username:<username> /password:<password> /namespace:<outputNamespace> /serviceContextName:<serviceContextName>
For the username parameter, type the user name that is used to sign in to Microsoft account or Microsoft Office 365. 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.
When you run the tool against Microsoft Dynamics CRM Online using the Microsoft account identity provider, you no longer have to supply the deviceid and devicepassword parameters from the command line. The tool registers your device automatically. However, you can manually register your device and obtain values for these parameters by first compiling and running the DeviceRegistration code sample as described here.
Open and build the DeviceRegistration project: SDK\Tools\DeviceRegistration\DeviceRegistration.csproj.
Run the executable file from the command line. To register your device, set the /operation parameter to Register.
Copy the displayed device ID and password values and use them as the deviceid and devicepassword parameter values when you run the CrmSvcUtil tool.
The following table lists the code generation tool parameters and a gives a brief description of their use.
Device ID used when you connect to the Online server for authentication. Only required if the device ID has not been stored on disk in the %USERPROFILE%\LiveDeviceID folder. Only used with the Microsoft account identity provider.
Device password that is used when you connect to the Online server for authentication. Only required if the device password has not been stored on disk in the %USERPROFILE%\LiveDeviceID folder. Only used with the Microsoft account identity provider.
The domain to authenticate against when you connect to the server.
The URL for the Organization service.
The file name for the generated code.
The language to generate the code in. This can be either “CS” or “VB”. The default value is “CS”.
The namespace for the generated code. The default is the global namespace.
The user name to use when you connect to the server for authentication.
The password to use when you connect to the server for authentication.
The name of the generated organization service context class. If no value is supplied, no service context is created.
Show usage information.
Suppress the banner at runtime.
Generate request and response classes for actions.
The following examples show how to use of the code generation tool from the command line for each deployment type. Note that user name and password are optional parameters. If your credentials for the target Microsoft Dynamics CRM server are stored in the Windows credential vault, you do not have to provide them to run the code generation tool.
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 Secure Sockets Layer (SSL).
The following sample shows how to run the code generation tool for Microsoft Dynamics CRM Online. The first example is for the Microsoft account identity provider and the second is for the Office 365 identity provider.
CrmSvcUtil.exe /url:https://myorg.api.crm.dynamics.com/XRMServices/2011/Organization.svc /out:GeneratedCode.cs /username:"email@example.com" /password:"myp@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.
To enable tracing when you run the tool, add the following lines to the configuration file:
Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online
Send comments about this topic to Microsoft.
© 2015 Microsoft. All rights reserved.