Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.slnfile using Visual Studio 2022Edit the
appsettings.jsonfile to set the following property values:Property Instructions UrlThe Url for your environment. Replace the placeholder https://yourorg.api.crm.dynamics.comvalue with the value for your environment. See View developer resources to find the Url for your environment.UserPrincipalNameReplace the placeholder you@yourorg.onmicrosoft.comvalue with the UPN value you use to access the environment.PasswordReplace the placeholder yourPasswordvalue with the password you use.Save the
appsettings.jsonfilePress 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-Matchheader with theinitialAcctETagValvalue.Request fails with
304 NotModifiedas expected.Update the record
telephone1column value.Attempt to retrieve the record again setting the
If-None-Matchheader with theinitialAcctETagValvalue.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-Matchheader with theinitialAcctETagValvalue.Request fails with
412 PreconditionFailedas expected.Attempt to update the original account setting the
If-Matchheader with theinitialAcctETagValvalue.Request fails with
412 PreconditionFailedas expected.Attempt to update the original account setting the
If-Matchheader with theupdatedAcctETagValvalue.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#)