Compartilhar via


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

 

Applies To: Dynamics CRM 2013

This walkthrough demonstrates how to write a simple console application that connects to Microsoft Dynamics CRM 2013 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.

    CrmSvcUtil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration" /out:Xrm\Xrm.cs /url:http://Crm/Contoso/XRMServices/2011/Organization.svc /domain:CONTOSO /username:administrator /password:pass@word1 /namespace:Xrm /serviceContextName:XrmServiceContext
    

Create a new console application project in Visual Studio

  1. Create a new console application named “ConsoleAppWalkthrough”.

    Create a project in Visual Studio

  2. Add the following references from the SDK\bin folder.

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

    • Microsoft.IdentityModel.dll

    • Microsoft.ServiceBus.dll

    • System.Data.Services.dll

    • System.Data.Services.Client.dll

    • System.Runtime.Serialization.dll

    If you don’t have the Microsoft.IdentityModel.dll assembly, you must install or enable Windows Identity Foundation. More information: Install Windows Identity Foundation (WIF) 3.5

  4. Make sure the console project specifies “.NET Framework 4” as the target framework and not “.NET Framework 4 Client Profile.” Check the properties of the project by selecting the Console 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 configuration file with your specific connection string.

    <connectionStrings>
      <add name="Xrm" connectionString="Server=http://crm/contoso; Domain=CONTOSO; Username=Administrator; Password=pass@word1" />
    
  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 2013
Portal walkthroughs (Dynamics CRM 2013)
Walkthrough: Build a web application that connects to Microsoft Dynamics CRM 2013 using developer extensions