Connect Power Apps Code Apps to Dataverse (PAC CLI + CRUD)
A hands-on guide to connecting a Power Apps Code app to Microsoft Dataverse with the PAC CLI: prerequisites, authenticating your environment, adding a Dataverse table as a data source, the auto-generated service and model files, and full create, read, update, and delete examples.
- Published
- Reading time
- 5 min read
What you’ll learn
- What you need
- Step 1 — Connect your environment
- Step 2 — Add a data source
- Step 3 — Generated files
- Step 4 — Create records
On this page (15 sections)
A Power Apps Code app is a pro-code React app that runs on the Power Platform — and its real power shows when you connect it to Microsoft Dataverse, the secure, scalable data backbone of the platform. This guide walks the full flow: from prerequisites, to authenticating your environment with the PAC CLI, to adding a Dataverse table as a data source, to performing complete CRUD operations with the auto-generated service and model files.
What you need
Before you start, make sure you have:
- Power Apps client library — the
@microsoft/power-appsclient library for code apps installed in your project. - PAC CLI 1.46+ — Power Apps (Power Platform) CLI version 1.46 or later.
- Dataverse environment — an environment with Dataverse enabled, and you must be connected to it using the PAC CLI.
Step 1 — Connect your environment
First, connect to your Dataverse environment using the PAC CLI. Create an authentication profile, then select it:
pac auth create --url https://<your-environment-url>
pac auth select --url https://<your-environment-url>
Tip: make sure you have access to the Dataverse environment before you connect — the profile you select is the one all later commands use.
Step 2 — Add a data source
Add a Dataverse table as a data source in your Power Apps Code app so you can work with its tables and columns. You can do this in Power Apps Studio or straight from the CLI.
Option A — Power Apps Studio
- Open Power Apps Studio and go to Data → Add data.
- Search for Dataverse and select it.
- Choose the table(s) you want to use — for example Accounts and Contacts.
- Click Add to add the Dataverse table to your app.
Option B — PAC CLI command
Make sure you're connected to your environment, then run pac code add-data-source with the table's logical name:
pac code add-data-source -a dataverse -t <table-logical-name>
# Example
pac code add-data-source -a dataverse -t account
Replace <table-logical-name> with the logical name of the Dataverse table you want to connect to (for example account). The data source is added to your project and the generated files are created for you.
Step 3 — Generated files
When you add a data source, Power Apps automatically generates a model file and a service file and places them under src/generated/. For the built-in Accounts table you get:
- AccountsModel.ts — defines the data model (schema and types) for the Accounts table, in
src/generated/models/. - AccountsService.ts — provides the service methods for interacting with the Accounts data, in
src/generated/services/.
Import the service and model into your code like this:
import { AccountsService } from './generated/services/AccountsService';
import type { Accounts } from './generated/models/AccountsModel';
The service methods are static — you call AccountsService.create(...) directly, without instantiating the class. Every call returns a result object whose data property holds the record(s).
Step 4 — Create records
Build a record object from the generated model, then submit it with the service. Include only the fields you want to populate — exclude system-managed or read-only columns such as the primary key (accountid) and ownership fields.
const newAccount = {
name: "NextM365 Technologies",
accountnumber: "ACC001"
};
try {
const result = await AccountsService.create(newAccount as Omit<Accounts, 'accountid'>);
if (result.data) {
console.log('Account created:', result.data);
}
} catch (err) {
console.error('Failed to create account:', err);
}
Step 5 — Read data
Retrieve a single record by its primary key, or query for multiple records.
Retrieve a single record (by ID)
const accountId = "00000000-0000-0000-0000-000000000000"; // actual ID
try {
const result = await AccountsService.get(accountId);
if (result.data) {
console.log('Account retrieved:', result.data);
}
} catch (err) {
console.error('Failed to retrieve account:', err);
}
Retrieve multiple records
Use getAll(). It accepts optional query options (select, filter, orderBy, top, skip, maxPageSize) for delegation and paging:
const options = {
select: ['name', 'accountnumber', 'address1_city'],
filter: "address1_country eq 'USA'",
orderBy: ['name asc'],
top: 50
};
try {
const result = await AccountsService.getAll(options);
const accounts = result.data || [];
console.log(`Retrieved ${accounts.length} accounts`);
} catch (err) {
console.error('Failed to fetch accounts:', err);
}
Tip: always limit the columns you retrieve with select — smaller payloads mean faster responses.
Step 6 — Update records
To update, you need the record's primary key and the changes to apply. Send only the properties you're changing — including unchanged values can trigger business logic or corrupt audit data.
const accountId = "your-account-guid";
const changes = {
name: "Updated Account Name",
telephone1: "555-0123"
};
try {
await AccountsService.update(accountId, changes);
console.log('Account updated successfully');
} catch (err) {
console.error('Failed to update account:', err);
}
Best practice: send only the fields you want to update. This reduces errors and improves performance.
Step 7 — Delete records
Delete a record using its primary key (record ID):
const accountId = "00000000-0000-0000-0000-000000000000"; // actual ID
try {
await AccountsService.delete(accountId);
console.log('Account deleted successfully');
} catch (err) {
console.error('Failed to delete account:', err);
}
Important: make sure the record exists before deleting. Note that deleting Dataverse data sources through the PAC CLI is not yet supported.
You're all set — the full CRUD workflow
You can now perform complete CRUD operations seamlessly against Dataverse from your Power Apps Code app:
- Connect — connect your code app to Dataverse.
- Add source — add a Dataverse table as a data source.
- Use services — use the generated services and models.
- Create — create new records in Dataverse.
- Read — retrieve single or multiple records.
- Update — update existing records with ease.
- Delete — delete records using the record ID.
The bottom line
Power Apps Code Apps + Dataverse give you the power to build scalable, secure, and high-performance business applications — combining pro-code flexibility with a governed, enterprise-grade data platform.
Keep learning on nextM365
Compare all options in connecting Code apps to any data source and learn the data layer itself in the Dataverse beginner guide.
Build smart. Automate more. Empower your business.
Related resources
Topics covered
PAC CLI · React · Connectors
Frequently asked questions
What do I need to connect a Power Apps Code app to Dataverse?
PAC CLI version 1.46 or later, access to a Dataverse environment, and the Power Apps client library installed in your project.
How do I add a Dataverse table as a data source?
Use Power Apps Studio (Data > Add data > Dataverse) or run pac code add-data-source -a dataverse -t <table-logical-name> from the PAC CLI, using the table logical name such as account.
Where does the CRUD code come from?
Power Apps auto-generates a model file (AccountsModel.ts) and a service file (AccountsService.ts) under src/generated for each table you add. You call the static methods create, get, getAll, update, and delete on the generated service, and read results from result.data.
Sources
- Microsoft Learn: Code Apps, Microsoft
- Microsoft Learn: Data Platform Intro, Microsoft
Have a Microsoft 365 topic idea?
Share article suggestions, community session ideas, corrections, or real-world scenarios for future nextM365 learning notes.
Keep learning Microsoft 365
Explore more practical tutorials for SharePoint, Power Platform, Copilot Studio, migration, automation, governance, and security.
Continue learning
Related tutorials
Related questions
Related comparisons