Using the Generated Classes in Your Project

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

The classes that are output by the CrmSvcUtil code generation tool are designed to be built into a class library that can be referenced by your projects that use Microsoft Dynamics CRM. This is the recommended approach. After you have generated classes for your organization using CrmSvcUtil, you should move the files to the folder for your Visual Studio project and then add them to the solution.

In order to work with the classes you also need to add references to the following Microsoft Dynamics CRM and .NET assemblies:

Add references to these assemblies Assembly location


For on-premises or IFD, browse to the SDK\Bin (32-bit) or SDK\Bin\64-bit folder.

For online, browse to the SDK\Bin\Online\64-bit folder. These assemblies work in both 32-bit and 64-bit environments.




From the SDK\Microsoft.Xrm\Bin folder.


From the .NET tab.

For all examples in this document, the XrmDataContext has been generated by CrmSvcUtil without using the /useDisplayNames switch. This means that the objects and properties will all be based on the logical names of the entities rather than the metadata labels. For example, the class will be called "account" rather than the display name "Account"; the contact First Name attribute will be called "firstname" rather than the display name "FirstName".

All of the code samples in this document were generated using the following connection string:

crmsvcutil /connectionString:"Authentication Type=Passport; Server=; User ID=user-wlid-email; Password=user-wlid-pwd; Device ID=user-defined-deviceid; Device Password=user-defined-devicepwd" /out:"Xrm.cs" /namespace:Xrm /dataContextPrefix:Xrm

Accessing the CrmService from a Plug-in

The following sample code shows how to get the Web service and generated classes from a plug-in.

/*Define the connection with the impersonated user*/
CrmConnection crmc = CrmConnection.Parse("Authentication Type=Passport; Server=https://" + org + "" + org + "; User ID=myuser; Password=mypassword; Device ID=mydeviceid; Device Password=mydevicepassword");
crmc.ImpersonatedUser = new Guid(userid);
var crmOnline = new DataContext(crmc);

List<opportunity> uplist = new List<opportunity>();

crmOnline.UsingService(service =>
  var query = from o in crmOnline.opportunityopportunities
  select o;

  foreach (opportunity thisopp in query)

See Also


© 2010 Microsoft Corporation. All rights reserved.