Getting Started

  • Digg
  • Facebook
  • LinkedIn
  • Twitter

In this walkthrough you will learn how to use XrmLinq to generate C# or VB.NET automatically then use that code in a simple Console Application.

If you are using the trial version please skip “Generating the Mapping” section and go directly to “Creating an Application” section.

Generating the Mapping

First we need to generate C# or VB.NET code by looking at the CRM Metadata, to do this open up the EntityMapper Tool.

  • If you have purchased the developer edition you can skip this step.Open “XrmLinq.Mapper.exe.config” file, locate the license key setting and copy paste license key we have allocated to you to the value attribute. Or use the License Manager that’s included with the zip file.
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="xrmlinq.license" value="your.license.key.here"/>
      </appSettings>
    </configuration>
  • Double click XrmLinq.Mapper.exe to open the Entity Mapper
  • XrmLinq - LINQ to CRM Entity Mapper
  • You will see a screen similar to one that is listed above
  • Organization name of your installation (this is case-sensitive, license is checked against this name)
  • If you are already authenticated against the domain this is not needed
  • Same as above
  • Same as above
  • Click on ‘Connect’, if the connection succeeds ‘Mapper Settings’ section will be activated.
  • “Use Smart Naming option is not available at this time, we’re working on stabilizing this feature at the moment.”

  • Enable LINQ will put attributes into the generated classes to make it work with our LINQ library
  • Enable WCF will put attributes into the generated classes to make it work with Windows Communication Foundation
  • Enter the namespace you’d like the generated classes to use
  • Select the language you want the code to be generated in. Options are: C# and VB.NET
  • If you have imported customizations or have done customizations there will be prefixes put in by CRM, if you’d like the generator to strip these off specify the prefixes here.To specify multiple prefixes use a comma to separate the entries.
  • Select a location for the generated file
  • Click on ‘Generate’ to begin the generation
 

Creating an Application

Once you have generated the mapping file we can now use this within an application. In this walkthrough we will create a simple Console Application.

  • Create a new Visual Studio project
  • Right click on the project -> Add Reference -> Browse to the XrmLinq.dll
  • Right click on the project -> Add Reference -> Browse to microsoft.crm.sdk.dll and microsoft.crm.sdktypeproxy.dll
  • Right click on the project -> Add Reference -> System.Web.Services
  • Right click on the project -> Add Reference -> System.Runtime.Serialization
  • Right click on the project -> Add Reference -> System.Configuration
  • NOTE: If you have purchased the developer edition you can skip this step.Add a new “Application Configuration” file if your project doesn’t already have one. Then add the following to it.
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <add key="xrmlinq.license" value="your.license.key.here"/>
      </appSettings>
    </configuration>
  • Right click on the project -> Add Existing Item -> Browse to the file you generated using the steps above.
    Trial Users; add the CrmEntities.cs file from the examples folder.
  • Build the project, it should build successfully, if it doesn’t; please email us the generated file and we will look into it.
  • Create an instance of CrmService by calling the Connection.Create method, and then create a CrmDataContext by passing the newly created CrmService instance. The example below assumes you’re already logged into the domain and can access crm with your credentials, to specify credentials explicitly use the overloaded methods in the Connection.Create method.�
    CrmService sdk = Connection.Create("http://host:port/mscrmservices/2007/crmservice.asmx", "OrgName");
    CrmDataContext context = new CrmDataContext(sdk);

    IMPORTANT: By default the provider will use QueryExpressions, QueryExpressions have limitations with some queries, specially joins and selecting multiple attributes from linked entities, therefore we recommend you using FetchXml.

    context.QueryThrough = XrmLinq.QueryThroughType.FetchXml;
  • Retrieve all accounts and order them by Name.
    var accounts = (from a in context.Accounts orderby a.Name select a).ToList();

Online Examples

© XrmLinq 2009.