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

  1. Clone or download the PowerApps-Samples repository.

  2. Open the QueryData.sln file using Visual Studio 2022.

  3. 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. Learn how to find your URL in View developer resources.
    UserPrincipalName Replace the placeholder you@yourorg.onmicrosoft.com value with the UPN value you use to access your environment.
    Password Replace the placeholder yourPassword value with the password you use.
  4. Save the appsettings.json file.

  5. Press F5 to run the sample.

Demonstrates

This sample has 11 sections:

  1. Create Records to query
  2. Selecting specific properties
  3. Using query functions
  4. Ordering and aliases
  5. Limit and count results
  6. Pagination
  7. Expanding results
  8. Aggregate results
  9. FetchXML queries
  10. Using predefined queries
  11. 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 $select against a contact entity to get the properties you want.
  • Include annotations to provide access to formatted values with the @OData.Community.Display.V1.FormattedValue annotation.

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 $filter and $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.maxpagesize request header to limit the number of rows returned.
  • Use the URL returned with the @odata.nextLink annotation to retrieve the next set of records.

Section 7: Expand results

Operations:

  • $expand with single-valued navigation properties.
  • $expand with partner property.
  • $expand with collection-valued navigation properties.
  • $expand with multiple navigation property types in a single request.
  • Nested $expand.
  • Nested $expand having 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 page and count attributes.

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.