共用方式為


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

 

Applies To: Dynamics CRM 2013

CrmSvcUtil.exe is a command-line code generation tool for use with Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online. 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.

In This Topic

About the code generation tool

Run the code generation tool

Parameters

Usage examples

Use the configuration File

Enable tracing

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.

  • Microsoft.Crm.Sdk.Proxy.dll

  • Microsoft.Xrm.Sdk.dll

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>

Use your Microsoft account or Microsoft Office 365 account user name for the username parameter. 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 /?

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.

Generate your individual device ID and password

  1. Open and build the DeviceRegistration project: SDK\Tools\DeviceRegistration\DeviceRegistration.csproj.

  2. Run the executable file from the command line. To register your device, set the /operation parameter to Register.

    C:\deviceregistration.exe /operation:Register
    
  3. Copy the displayed device ID and password values and use them as the deviceid and devicepassword parameter values when you run the CrmSvcUtil tool.

Parameters

The following table lists the code generation tool parameters and a gives a brief description of their use.

Parameter

Shortcut

Description

Required

deviceid

di

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.

False

devicepassword

dp

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.

False

domain

d

The domain to authenticate against when you connect to the server.

False

url

The URL for the Organization service.

True

out

o

The file name for the generated code.

True

language

l

The language to generate the code in. This can be either “CS” or “VB”. The default value is “CS”.

False

namespace

n

The namespace for the generated code. The default is the global namespace.

False

username

u

The user name to use when you connect to the server for authentication.

False

password

p

The password to use when you connect to the server for authentication.

False

servicecontextname

The name of the generated organization service context class. If no value is supplied, no service context is created.

False

help

?

Show usage information.

False

nologo

Suppress the banner at runtime.

False

generateActions

Generate request and response classes for actions.

Usage examples

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.

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 Secure Sockets Layer (SSL).

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

Microsoft Dynamics CRM Online

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:"myname@live.com" /password:"myp@ssword!" 

CrmSvcUtil.exe /url:https://myorg.api.crm.dynamics.com/XRMServices/2011/Organization.svc    /out:GeneratedCode.cs /username:"myname@myorg.onmicrosoft.com" /password:"myp@ssword!" 

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 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.

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

Enable tracing

To enable tracing when you run the tool, add the following lines to the configuration file:

<system.diagnostics> 
   <trace autoflush="false" indentsize="4"> 
      <listeners> 
         <add name="configConsoleListener" type="System.Diagnostics.ConsoleTraceListener"> 
            <filter type="System.Diagnostics.EventTypeFilter" initializeData="Error" /> 
         </add> 
      </listeners> 
   </trace> 
</system.diagnostics>

See Also

Developer tools
Browse the metadata for your organization
Create extensions for the code generation tool
Use the early-bound entity classes for create, update, and delete
Troubleshooting tips
Run a simple program using Microsoft Dynamics CRM 2013 web services