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 article walks through how to set up a blank app from Vite and turn it into a Power Apps code app. It covers configuring a TypeScript app using the Power Platform SDK
Note
Preview features aren’t meant for production use and may have restricted functionality. These features are available before an official release so that customers can get early access and provide feedback.
Prerequisites
- Power Platform environment with code apps enabled
- Visual Studio Code
- Node.js Long term support (LTS) version
- Power Platform Tools for VS Code
Initialize your Vite App
Open Visual Studio Code and open a new PowerShell terminal and enter:
mkdir C:\CodeApps -Force cd C:\CodeApps npm create vite@latest AppFromScratch -- --template react-ts cd C:\CodeApps\AppFromScratch npm installIf asked, agree to install
create-viteAccept the default package name
appfromscratchby pressing Enter.If asked to select a framework, select React.
If asked to select a variant, select TypeScript.
At this time, the Power SDK requires the port to be 3000 in the default configuration.
Install the node type definition using:
npm i --save-dev @types/nodeOpen the
vite.config.ts, and update to be:import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' import * as path from 'path' // https://vite.dev/config/ export default defineConfig({ base: "./", server: { host: "::", port: 3000, }, plugins: [react()], resolve: { alias: { "@": path.resolve(__dirname, "./src"), }, }, });Save the file.
Enter the following to test your Vite app:
npm run devNote
If you're developing on macOS, you might need to update package.json to not reference
start vite. For instance you'd change the dev entry fromstart vite && start pac code run:"scripts": { "dev": "start vite && start pac code run", "build": "tsc -b && vite build", "lint": "eslint .", "preview": "vite preview" }to
vite && pac code run"scripts": { "dev": "vite && pac code run", "build": "tsc -b && vite build", "lint": "eslint .", "preview": "vite preview" }The template project starts and runs locally. Browse to the
http://localhost:3000address given.
Important
If you don't see port 3000, then revisit the previous steps.
Press Ctrl + C to stop the local server.
Initialize your code app
Authenticate the Power Platform CLI against your Power Platform tenant:
pac auth createSign in using your Power Platform account when prompted.
Note
You can also use the Power Platform Tools VS Code Extension to do authenticate.
Select your environment using:
pac env select -env <URL of your environment>You can also use the Power Platform Tools VS Code Extension to select the environment.
Initialize your code app using:
pac code init --displayName "App From Scratch"Notice that there's now a
power.config.jsonfile in your project.Install the Power SDK using:
npm install --save "@microsoft/power-apps"Open the
package.json, and update the existing line:"dev": "vite"Change it to:
"dev": "start pac code run && vite",Save the updated
package.json.Add a new file under the
srcfolder namedPowerProvider.tsxand grab the code from github.com/microsoft/PowerAppsCodeApps/docs/assets/PowerProvider.tsxSave the file.
Open
main.tsxand add the following import under the existing imports:import PowerProvider from './PowerProvider.tsx'Update
main.tsx:<StrictMode> <App /> </StrictMode>,Change it to:
<StrictMode> <PowerProvider> <App /> </PowerProvider> </StrictMode>,Save the file.
You can now test the code app by using:
npm run devThis runs the Vite server, but also starts the Power SDK server:
Open the URL provided by the Power SDK Server.
Important
Open the URL in the same browser profile as your Power Platform tenant.
You should see the app open similar to: