Web API PowerShell Query Data sample
This PowerShell sample demonstrates how to query Dataverse data using:
- OData query syntax ($select, $filter, $orderby, $top, $count, $expand, $apply)
- Query functions and operators
- Parameter aliases
- Pagination with nextLink
- Aggregation
- FetchXML queries
- Predefined queries (saved queries and user queries)
This sample uses the Dataverse Web API PowerShell Helper functions to manage authentication and provide re-usable functions to perform common operations. These scripts are referenced using dot sourcing with the following lines:
. $PSScriptRoot\..\Core.ps1
. $PSScriptRoot\..\TableOperations.ps1
. $PSScriptRoot\..\CommonFunctions.ps1
Note
This sample should work with Windows, Linux, and macOS, but has only been tested on Windows.
Prerequisites
Before running this sample you should read these articles that explain concepts and patterns used by these samples:
- Quick Start Web API with PowerShell and Visual Studio Code
- Use PowerShell and Visual Studio Code with the Dataverse Web API
- Query data using the Web API
This sample requires:
Visual Studio Code. See Download Visual Studio Code
PowerShell extension for Visual Studio Code. See PowerShell for Visual Studio Code
PowerShell 7.4 or higher. See Install PowerShell on Windows, Linux, and macOS
Az PowerShell module version 11.1.0 or higher. See How to install Azure PowerShell
To update an existing installation to the latest version, use
Update-Module -Name Az -ForceAccess to Dataverse with privileges to perform data operations.
How to run the sample
Clone or download the PowerApps-Samples repository.
Open the QueryData.ps1 file using Visual Studio Code
Edit this line to use the URL of the environment you want to connect to:
Connect 'https://yourorg.crm.dynamics.com/' # change this(Optional) Set the
$deleteCreatedRecordsvariable to$falseif you do not want to delete the records this sample creates.Press F5 to run the sample.
The first time you run the sample a browser window opens. In the browser window, enter or select the credentials you want to use to authenticate.
To connect as a different user, run the Disconnect-AzAccount command.
Demonstrates
This sample has 9 sections:
Setup: Create records to query
Operations: Create sample data for the query demonstrations.
- Create one account record named 'Contoso, Ltd. (sample)' with the following related records:
- A primary contact: Yvonne McKay
- 8 related contacts in the
contact_customer_accountscollection - 3 tasks for the account
- 3 tasks for each contact (27 total tasks)
All records are added to the $recordsToDelete array for cleanup.
Section 1: Select specific properties
Operations: Use $select to retrieve specific columns from a record.
- Retrieve
fullname,jobtitle, andannualincomefor a contact using theGet-Recordfunction - Display formatted values using OData annotations (e.g.,
annualincome@OData.Community.Display.V1.FormattedValue)
Section 2: Use query functions
Operations: Use OData query functions and operators in $filter expressions.
- Filter records using
containsfunction - Filter records using
Microsoft.Dynamics.CRM.LastXHoursquery function - Use comparison operators (
gt- greater than) - Control filter precedence using parentheses with
and/oroperators
Section 3: Ordering and aliases
Operations: Sort query results and use parameter aliases.
- Order results using
$orderbywith ascending and descending sort - Use parameterized aliases (e.g.,
@p1,@p2) to make queries more maintainable
Section 4: Limit and count results
Operations: Limit the number of results and get counts.
- Limit results using
$top - Get a count of records using
/$count - Include count with results using
$count=true
Section 5: Pagination
Operations: Retrieve results across multiple pages.
- Use
maxPageSizeparameter to control page size - Use
@odata.nextLinkto retrieve subsequent pages using theGet-NextLinkfunction
Section 6: Expand results
Operations: Retrieve related records using $expand.
- Expand single-valued navigation properties (e.g.,
primarycontactid) - Expand collection-valued navigation properties (e.g.,
contact_customer_accounts) - Expand multiple navigation properties in a single request
- Perform nested expands of single-valued navigation properties
- Perform nested expands with both single-valued and collection-valued navigation properties
Section 7: Aggregate results
Operations: Perform aggregations using $apply.
- Calculate aggregate values (average, sum, min, max) for numeric columns
- Display formatted aggregate values
Section 8: FetchXML queries
Operations: Query data using FetchXML instead of OData.
- Execute basic FetchXML queries with filters and ordering
- Implement simple paging using
pageandcountattributes - Implement paging with paging cookies to handle large datasets:
- Extract and decode the
@Microsoft.Dynamics.CRM.fetchxmlpagingcookieannotation - Update FetchXML with the paging cookie for subsequent pages
- Loop through all pages until
@Microsoft.Dynamics.CRM.morerecordsis false
- Extract and decode the
Section 9: Use predefined queries
Operations: Use saved queries and user queries.
- Retrieve and execute a system saved query (e.g., 'Active Accounts') using
savedQueryparameter - Create a user query (personal view) using the
New-Recordfunction - Execute the user query using
userQueryparameter
Cleanup: Delete sample records
Operations: A reference to each record created in this sample was added to a list as it was created.
When the $deleteCreatedRecords variable is set to $true, this section loops through that list and deletes each record using the Remove-Record function.
Clean up
By default, this sample deletes all the records it created. If you want to view created records after the sample is complete, change the $deleteCreatedRecords variable to $false.