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 Query Data Sample and uses the common C# constructs described in Web API Samples (C#).
Prerequisites
The following is 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/QueryData/ folder.
Open the
QueryData.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 this.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/QueryData/Program.cs
Demonstrates
This sample has 11 regions:
Section 0: Create Records to query
Operations: Create 1 account record with 9 related contact records. Each contact has 3 related task records.
This is the data that will be used in this sample.
Section 1 Selecting specific properties
Operations:
- Using
$selectagainst a contact entity to get the properties you want. - Including annotations provides access to formatted values with the
@OData.Community.Display.V1.FormattedValueannotation
Section 2 Using query functions
Operations:
- Using standard query functions (
contains,endswith,startswith) to filter results. - Using Dataverse query functions (
LastXhours,Last7Days,Today,Between,In) - Using filter operators and logical operators (
eq,ne,gt,and,or) - Set precedence using parenthesis
((criteria1) and (criteria2)) or (criteria3)
Section 3 Ordering and aliases
Operations:
- Using
$orderby - Using parameterized aliases (
?@p1=fullname) with$filterand$orderby
Section 4 Limit and count results
Operations:
- Limiting results using
$top. - Get a count value using
$count.
Section 5 Pagination
Operations:
- Use the
Prefer: odata.maxpagesizerequest header to limit the number of rows returned. - Use the url returned with the
@odata.nextLinkannotation to retrieve the next set of records.
Section 6 Expanding results
Operations:
$expandwith single-valued navigation properties.$expandwith partner property.$expandwith collection-valued navigation properties.$expandwith multiple navigation property types in a single request.- Nested
$expand. - Nested
$expandhaving both single-valued and collection-valued navigation properties.
Section 7 Aggregate results
Operations: Using $apply=aggregate with average, sum, min, & max.
Section 8 FetchXML queries
Operations:
- Sending requests using fetchXml using
?fetchXml= - Paging using the
pageandcountattributes.
Section 9 Using predefined queries
Operations:
- Using
{entitysetname}?savedQuery={savedqueryid}to return the results of a saved query (system view) - Using
{entitysetname}?userQuery={userquery}to return the results of a user query (saved view)
Section 10: Delete sample records
Operations: A reference to each record created in this sample was added to a list as it was created. In this sample the records are deleted using a $batch operation.
See also
Query Data using the Web API
Web API Query Data Sample
Use the Dataverse Web API
Web API Basic Operations 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#)