Web API Basic Operations Sample (C#)

This .NET 6.0 sample demonstrates how to perform common data operations using the Dataverse Web API.

This sample uses the common helper code in the WebAPIService class library (C#).

Note

This sample implements the Dataverse operations and console output detailed in Web API Basic Operations Sample and uses the common C# constructs described in Web API Samples (C#).

Prerequisites

These are required to build and run this sample:

  • Microsoft Visual Studio 2022.
  • Access to Dataverse with privileges to perform data operations.

How to run this sample

  1. Clone or download the PowerApps-Samples repository.

  2. Locate the /dataverse/webapi/C#-NETx/BasicOperations/ folder.

  3. Open the BasicOperations.sln file using Visual Studio 2022

  4. Edit the appsettings.json file to set the following property values:

    Property Instructions
    Url The Url for your environment. Replace the placeholder https://yourorg.api.crm.dynamics.com value with the value for your environment. See View developer resources to find the Url for your environment.
    UserPrincipalName Replace the placeholder [email protected] value with the UPN value you use to access the environment.
    Password Replace the placeholder yourPassword value with the password you use.
  5. Save the appsettings.json file

  6. Press F5 to run the sample.

Code

The code for this sample is here: PowerApps-Samples/dataverse/webapi/C#-NETx/BasicOperations/Program.cs

Demonstrates

This sample has five regions:

Section 1: Basic Create and Update operations

Operations:

  • Create a contact record.
  • Update the contact record.
  • Retrieve the contact record.
  • Update a single property of the contact record.
  • Retrieve a single property of the contact record.

Section 2: Create record associated to another

Operations: Associate a new record to an existing one.

Operations: Create the following entries in one operation: an account, its associated primary contact, and open tasks for that contact. These entity types have the following relationships:

Accounts
    |---[Primary] Contact (N-to-1)
        |---Tasks (1-to-N)

Section 4: Associate and Disassociate entities

Operations:

  • Add a contact to the account contact_customer_accounts collection.
  • Remove a contact from the account contact_customer_accounts collection.
  • Associate a security role to a user using the systemuserroles_association collection.
  • Remove a security role for a user using the systemuserroles_association collection.

Section 5: Delete sample entities

Operations: A reference to each record created in this sample was added to a list as it was created. This section loops through that list and deletes each record.

Clean up

By default this sample deletes all the records created in it. If you want to view created records after the sample is completed, change the deleteCreatedRecords variable to false and you'll be prompted to decide if you want to delete the records.

See also

Use the Dataverse Web API
WebAPIService class library (C#)
Create a table row using the Web API
Update and delete table rows using the Web API
Retrieve an table row using the Web API
Web API Samples
Web API Basic Operations Sample
Web API Query Data sample (C#)
Web API Conditional Operations sample (C#)
Web API Functions and Actions Sample (C#)
Web API table schema operations sample (C#)
Web API WebApiService Parallel Operations Sample (C#)
Web API Parallel Operations with TPL Dataflow components Sample (C#)