Jaa


Walkthrough: Build a console application that connects to Microsoft Dynamics CRM 2015 using developer extensions

 

Applies To: Dynamics CRM 2015

This walkthrough demonstrates how to write a simple console application that connects to Microsoft Dynamics CRM 2015 using Developer Extensions and creates a contact record.

You can find the sample code that this walkthrough produces in the Walkthroughs\Portal\ConsoleAppWalkthrough folder.

In This Topic

Generate early bound types

Create a new console application project in Visual Studio

Generate early bound types

  1. Run the CrmSvcUtil.exe tool, with the “Microsoft.Xrm.Client.CodeGeneration” extension, to generate your entity classes and service contexts. The following is an example command to create a file called Xrm.cs that points at an instance of Microsoft Dynamics CRM. Note that the Microsoft.Xrm.Client.CodeGeneration.dll file must be in the same directory as the CrmSvcUtil.exe file, or in the system global assembly cache, when you run this command. The first command shown is for an on-premises organization. The second command is for a CRM Online organization. Both commands should be executed as a single command line with no line breaks.

    
        CrmSvcUtil.exe
        /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration"/out:Xrm.cs /url:http://<myserver>/<myorg>/XRMServices/2011/Organization.svc 
        /domain:<mydomain> /username:<myusername> /password:<mypassword> /namespace:Xrm /serviceContextName:XrmServiceContext
    
    
        CrmSvcUtil.exe
        /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration"
        /out:Xrm.cs /url:https://<mydomain>.api.crm.dynamics.com/XRMServices/2011/Organization.svc
        /username:<myusername>@<mydomain>.onmicrosoft.com /password:<mypassword> /namespace:Xrm /serviceContextName:XrmServiceContext
    

    Tip

    The CrmSvcUtil tool is available in the Bin folder of the SDK download or by installing the Microsoft.CrmSdk.CoreTools NuGet package.

Create a new console application project in Visual Studio

  1. Create a new console application named “ConsoleAppWalkthrough”. If you don’t already have the .NET Framework 4.5.2 installed, you can obtain the Developer Pack from Installing the .NET Framework.

    Create a project in Visual Studio

  2. Add the following references from the SDK\bin folder. You can skip this step and the next by simply installing the Microsoft.CrmSdk.Extensions NuGet package.

    • AntiXSSLibrary.dll

    • Microsoft.Crm.Sdk.Proxy.dll

    • Microsoft.Xrm.Client.dll

    • Microsoft.Xrm.Portal.dll

    • Microsoft.Xrm.Portal.Files.dll

    • Microsoft.Xrm.Sdk.dll

  3. Add the following references from .NET.

    • System.IdentityModel.dll

    • Microsoft.ServiceBus.dll

    • System.Data.Services.dll

    • System.Data.Services.Client.dll

    • System.Runtime.Serialization.dll

  4. Make sure the console project specifies .NET Framework 4.5.2 as the target framework and not .NET Framework 4.5.2 Client Profile. Check the properties of the project by selecting the ConsoleAppWalkthrough project in Solution Explorer and clicking Alt-Enter.

    Set target framework

  5. Right-click the project in Visual Studio, click Add, and then click Existing Item.

  6. Select the “xrm.cs” file that you created when you generated the early bound types.

  7. Right-click your project again, click Add, and then click New Item.

  8. Select Application Configuration File from the options and then click Add.

  9. Edit the App.config configuration file with your specific connection string.

    <?xml version="1.0"?>
    <configuration>
      <configSections>
        <section name="microsoft.xrm.client" type="Microsoft.Xrm.Client.Configuration.CrmSection, Microsoft.Xrm.Client"/>
      </configSections>
      <connectionStrings>
         <add name="Xrm" connectionString="Server=http://crmserver/contoso; Domain=CONTOSO; Username=Administrator;
         Password=pass@word1"/>
      </connectionStrings>
      <microsoft.xrm.client>
        <contexts default="Xrm">
          <add name="Xrm" type="Xrm.XrmServiceContext, Xrm" connectionStringName="Xrm"/>
        </contexts>
      </microsoft.xrm.client>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
      </startup>
    </configuration>
    
  10. Add a using statement to the namespace that you used in Step 1 when you created the project (for this example “Xrm”).

  11. Add the following code to your program class:

    static void Main(string[] args)
    {
        var xrm = new XrmServiceContext("Xrm");
    
        WriteExampleContacts(xrm);
    
        //Create a new contact called Allison Brown.
        var allisonBrown = new Xrm.Contact
        {
            FirstName = "Allison",
            LastName = "Brown",
            Address1_Line1 = "23 Market St.",
            Address1_City = "Sammamish",
            Address1_StateOrProvince = "MT",
            Address1_PostalCode = "99999",
            Telephone1 = "12345678",
            EMailAddress1 = "allison.brown@example.com"
        };
    
        xrm.AddObject(allisonBrown);
        xrm.SaveChanges();
    
        WriteExampleContacts(xrm);
    
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
    
    /// <summary>
    /// Use all contacts where the email address ends in @example.com.
    /// </summary>
    private static void WriteExampleContacts(XrmServiceContext xrm)
    {
        var exampleContacts = xrm.ContactSet
            .Where(c => c.EMailAddress1.EndsWith("@example.com"));
    
        //Write the example contacts.
        foreach (var contact in exampleContacts)
        {
            Console.WriteLine(contact.FullName);
        }
    }
    
  12. Debug and test the solution. This will create a contact in your CRM organization.

See Also

Portal developer guide for Microsoft Dynamics CRM 2015
Portal walkthroughs for Dynamics CRM 2015
Walkthrough: Build a web application that connects to Microsoft Dynamics CRM 2015 using developer extensions

© 2016 Microsoft. All rights reserved. Copyright