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
Clone or download the PowerApps-Samples repository.
Locate the /dataverse/webapi/C#-NETx/ConditionalOperations/ folder.
Open the
ConditionalOperations.sln
file using Visual Studio 2022Edit 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 you@yourorg.onmicrosoft.com
value with the UPN value you use to access the environment.Password
Replace the placeholder yourPassword
value with the password you use.Save the
appsettings.json
filePress 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:
Attempts to retrieve the record setting the
If-None-Match
header with theinitialAcctETagVal
value.Request fails with
304 NotModified
as expected.Update the record
telephone1
column value.Attempt to retrieve the record again setting the
If-None-Match
header with theinitialAcctETagVal
value.Request succeeds because the initial ETag value doesn't match anymore.
Store the new ETag value as
updatedAcctETagVal
.
Section 2: Optimistic concurrency on delete and update
Operations:
Attempt to delete the original account setting the
If-Match
header with theinitialAcctETagVal
value.Request fails with
412 PreconditionFailed
as expected.Attempt to update the original account setting the
If-Match
header with theinitialAcctETagVal
value.Request fails with
412 PreconditionFailed
as expected.Attempt to update the original account setting the
If-Match
header with theupdatedAcctETagVal
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#)