Quick Start: Organization service sample (C#)

This is where you begin working with the .NET SDK assemblies to work with data using Common Data Service for Apps.

In this quick start you will create a minimum console application to connect to the Organization service using the CrmServiceClient class. You will pass your connection information using a connection string passed to the constructor.

You will use the IOrganizationService.Execute method passing an instance of the WhoAmIRequest class, and you will display the WhoAmIResponse.UserId value.

Note

This quick start example does not include error handling. This is a minimum example of what you need to connect to and use the Organization service.

Prerequisites

  • Visual Studio (2017 recommended)
  • Internet connection
  • Valid user account for a Common Data Service for Apps instance
    • Your username
    • Your password
  • Url to the CDS for Apps environment you want to connect with
  • Basic understanding of the Visual C# language

Create Visual Studio project

  1. Create a new Console App (.NET Framework) project using .NET Framework 4.6.2

    Start a console app project

    Note

    This screenshot shows the name OrgServiceQuickStart, but you can choose to name the project and solution whatever you want.

  2. In Solution Explorer, right-click the project you created and select Manage NuGet Packages... in the context menu.

    Add NuGet package

  3. Browse for the latest version of the Microsoft.CrmSdk.XrmTooling.CoreAssembly NuGet package and install it.

    Install Microsoft.CrmSdk.XrmTooling.CoreAssembly NuGet package

Note

You must select I Accept in the Licence Acceptance dialog.

Edit Program.cs

  1. Add these using statements to the top of Program.cs

    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Tooling.Connector;
    
  2. Replace the Main method with the following code:

    static void Main(string[] args)
    {            
        // e.g. https://yourorg.crm.dynamics.com
        string url = "<your environment url>";
        // e.g. you@yourorg.onmicrosoft.com
        string userName = "<your user name>";
        // e.g. y0urp455w0rd
        string password = "<your password>";
    
        string conn = $@"
        Url = {url};
        AuthType = Office365;
        UserName = {userName};
        Password = {password};
        RequireNewInstance = True";
    
        using (var svc = new CrmServiceClient(conn))
        {
    
            WhoAmIRequest request = new WhoAmIRequest();
    
            WhoAmIResponse response = (WhoAmIResponse)svc.Execute(request);
    
            Console.WriteLine("Your UserId is {0}", response.UserId);
    
            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }
    }
    
  3. Edit the following values to add information for your environment:

    // e.g. https://yourorg.crm.dynamics.com
    string url = "<your environment url>";
    // e.g. you@yourorg.onmicrosoft.com
    string userName = "<your user name>";
    // e.g. y0urp455w0rd
    string password = "<your password>";
    

Run the program

  1. Press F5 to run the program. The output should look like this:

    Your UserId is 969effb0-98ae-478c-b547-53a2968c2e75
    Press any key to exit.
    

Congratulations!

You have successfully connected to the organization service.

Next Steps

These topics will explain how to work with CDS for Apps entities:

Entity Operations using the Organization service
Create entities using the Organization Service
Retrieve an entity using the Organization Service
Update and Delete entities using the Organization Service
Associate and disassociate entities using the Organization Service