Web API Conditional 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 Conditional 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/ConditionalOperations/ folder.

  3. Open the ConditionalOperations.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/ConditionalOperations/Program.cs

Demonstrates

This sample has four regions:

Section 0: Create sample records

Operations:

  • Creates a single account record.
  • Retrieves the record and stores the ETag value as initialAcctETagVal.

Section 1: Conditional GET

Operations:

  1. Attempts to retrieve the record setting the If-None-Match header with the initialAcctETagVal value.

    Request fails with 304 NotModified as expected.

  2. Update the record telephone1 column value.

  3. Attempt to retrieve the record again setting the If-None-Match header with the initialAcctETagVal value.

    Request succeeds because the initial ETag value doesn't match anymore.

  4. Store the new ETag value as updatedAcctETagVal.

Section 2: Optimistic concurrency on delete and update

Operations:

  1. Attempt to delete the original account setting the If-Match header with the initialAcctETagVal value.

    Request fails with 412 PreconditionFailed as expected.

  2. Attempt to update the original account setting the If-Match header with the initialAcctETagVal value.

    Request fails with 412 PreconditionFailed as expected.

  3. Attempt to update the original account setting the If-Match header with the updatedAcctETagVal value.

    Request succeeds because the ETag value matches the current value for the record.

Section 3: Delete sample records

Operations: A reference to each record created in this sample was added to a list as it was created. This section sends a $batch request to delete the 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

Perform conditional operations using the Web API
Use the Dataverse Web API
WebAPIService class library (C#)
Web API Samples
Web API Basic Operations Sample (C#)
Web API Query Data 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#)