Web API Query Data sample
This .NET 6.0 sample demonstrates how to use capabilities to query data using the Dataverse Web API.
This sample uses the common helper code in the WebAPIService class library project.
Prerequisites
- Microsoft Visual Studio 2022
- Access to Dataverse with privileges to perform data operations
How to run the sample
Clone or download the PowerApps-Samples repository.
Open the QueryData.sln file using Visual Studio 2022.
Edit the appsettings.json file 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. Learn how to find your URL in View developer resources.UserPrincipalNameReplace the placeholder you@yourorg.onmicrosoft.comvalue with the UPN value you use to access your environment.PasswordReplace the placeholder yourPasswordvalue with the password you use.Save the
appsettings.jsonfile.Press
F5to run the sample.
Demonstrates
This sample has 11 sections:
- Create Records to query
- Selecting specific properties
- Using query functions
- Ordering and aliases
- Limit and count results
- Pagination
- Expanding results
- Aggregate results
- FetchXML queries
- Using predefined queries
- Delete sample records
Section 1: Create records to query
Operations: Create one account record with nine related contact records. Each contact has three related task records.
This created data is used in the rest of this sample.
Section 2: Select specific properties
Operations:
- Use
$selectagainst a contact entity to get the properties you want. - Include annotations to provide access to formatted values with the
@OData.Community.Display.V1.FormattedValueannotation.
Section 3: Use query functions
Operations:
- Use standard query functions (
contains,endswith,startswith) to filter results. - Use Dataverse query functions (
LastXhours,Last7Days,Today,Between,In). - Use filter operators and logical operators (
eq,ne,gt,and,or). - Set precedence using parenthesis
((criteria1) and (criteria2)) or (criteria3).
Section 4: Ordering and aliases
Operations:
- Use
$orderby. - Use parameterized aliases (
?@p1=fullname) with$filterand$orderby.
Section 5: Limit and count results
Operations:
- Limit results using
$top. - Get a count value using
$count.
Section 6: 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 7: Expand 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 8: Aggregate results
Operations: Use $apply=aggregate with average, sum, min, & max.
Section 9: FetchXML queries
Operations:
- Send requests using fetchXml using
?fetchXml=. - Simple paging using
pageandcountattributes.
Section 10: Use predefined queries
Operations:
- Use
{entitysetname}?savedQuery={savedqueryid}to return the results of a saved query (system view). - Use
{entitysetname}?userQuery={userquery}to return the results of a user query (saved view).
Section 11: 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 using the WebAPIService BatchRequest class.
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're prompted to delete the records, if desired.