banner

Power Apps Code Apps allows developers to bring custom React applications into the Power Platform, integrated with key Power Platform services like Dataverse, connectors, and Entra ID authentication.

This article takes you through the basics of creating your first code app, step-by-step!

The Plan

Image 2 We will follow an incremental 6-step plan to get you up and running with your first code app. While AI tools like GitHub Copilot and others have shown incredible capabilities in handling the entire end-to-end process (or at least a good chunk of it), we will walk through each, step by step so you understand what is going on in each step.

Install Dependencies

Image 3 First, you must install several dependencies onto your machine:

  • VS Code(or any IDE of your choice!)
    • https://code.visualstudio.com/download
  • NodeJS
    • JavaScript runtime, required for developing React apps
    • https://nodejs.org/en
  • Power Platform PAC CLI
    • Required to eventually “push” the Code App to your Environment
    • https://learn.microsoft.com/en-us/power-platform/developer/howto/install-cli-msi
    • This tutorial assumes you have already authenticated into your PAC CLI. See how to here via the pac auth command if you have not done so already.
  • GitHub Copilot
    • Within VS Code (as extension) or GitHub Copilot CLI
    • You don’t necessarily need this to make a Code App… as long as you are a good React dev 
    • https://code.visualstudio.com/docs/copilot/overview

Enable Code Apps in your Environment

Image 4 Before we begin building apps, it is important to note that as of the time of this writing you have to enable code apps in your target Power Platform environment. For whatever environment you wanted to host your first Code App in, visit the PPAC and find the "Power Apps Code Apps" toggle in the environment's settings!

Create a React App

We are now ready for the first step in app creation: make a React app! Run npm create vite@latest Cat-Facts in your terminal to scaffold a "boilerplate" React app. Image 5

After you do so, you should see it instantly serve the app for you on localhost. It provides a URL for you to navigate to in a browser to see your app. Image 6

Use AI to Customize your React App

With a basic React app now scaffolded, we can now proceed to build a React app that actually does something for us! If you are a talented React developer, you can certainly get to work writing your code at this stage. But for people like me that aren't familiar React code, you can use AI to assist with development. Below I'm running copilot in the terminal to open GitHub Copilot to assist with writing my app.

Image 7

Once GitHub Copilot loads, I am running a simple prompt like the following:

Make me a “Random Cat Facts” app. When a user clicks on a button, they are shown a random cat fact.

Image 8

And allowing it to do its thing, it confirms it is complete!

Image 9

Next, run npm run dev in the terminal to test out your new React app!

Image 10

Vite will now serve the React app to you on a local server for you to navigate to in the browser to test. Image 11 Image 12

Prepare your React App to be a Power Apps Code App

Congrats, you have made your first React app! But the React app is not ready to be a Power Apps Code App just yet. To integrate the React app with Power Platform services (and allow it to be hosted as a Power Apps Code App), there are a few remaining steps:

First, install two packages in your React app from npm:

  1. Add the @microsoft/power-apps npm package to the project by running npm install @microsoft/power-apps in your terminal.
  2. Add the @microsoft/power-apps-vite npm package to the project by running npm install @microsoft/power-apps-vite in your terminal.

Image 13

After both packages install, add the following highlighted portions to your vite.config.ts file in your app (see screenshot). For reference, I've included the entire contents of my file after the changes below (you can just replace your entire vite.config.ts file with this).

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { powerApps } from "@microsoft/power-apps-vite/plugin"

// https://vite.dev/config/
export default defineConfig({
  plugins: [react(), powerApps()],
});

Image 14

And then run pac code init -n "Fun Cat Facts" in your terminal to set up the app's power.config.json file with Power Apps-related information about the app.

Image 15

Great! Your React app is now a Code App! Run npm run dev again to test it out. This time, you will see there is also a Power Apps "Local Play" URL. You can navigate to this URL in your browser to test out your code app as a Power Apps Code App!

Image 16 Image 17

Push to your Environment

After testing locally, run npm run build followed by pac code push to push your code app to your target Power Platform environment!

Image 18

The PAC CLI will confirm it pushed successfully and provide a URL that you can navigate to to play it.

Image 19

Connect with Dataverse

Ok - this Cat Facts app is fun, but how do we make it real? How can we, for example, connect our app to Dataverse and perform Create, Read, Update, Delete (CRUD) operations on it?

Run pac code add-data-source –a dataverse –t contact to connect your app with the Contacts Dataverse Table. Image 20

You will see running this command will add schema and service files to your app that allow the app to interface with the data in the Contacts table. Image 21

Microsoft provides terrific documentation on how code in your Code App can be written to interface with data in the Contacts table. See here if interested in reviewing yourself: How to: Connect your code app to Dataverse

Image 22

Since I'll be relying on GitHub Copilot to write my app's code for me, I'll make sure to provide this documentation to the AI before instructing it to make my app for me. By explicitly providing this documentation, I am ensuring GitHub Copilot has the latest documentation and best practices to follow.

Provide the following prompt to GitHub Copilot:

First, read this documentation on how to interface with
Dataverse: https://learn.microsoft.com/en-us/power-apps/developer/code-apps/how-to/connect-to-dataverse

I have already added the Contact table to this app.

Please make another page of this app in which it allows a user to Create, Read, Update, Delete records in the Contact table

Image 23

Again, after allowing GitHub Copilot to do its thing for several minutes, it confirms it has completed the work!

Image 24

Next step is to test out the app: run npm run dev and Vite will serve up your app locally. Navigate to the Power Apps "Local Play" URL in your browser.

Image 25

Your app should be updated with an additional page that allows you to interface with data in your Contacts table.

Image 26

Push to your Environment

And, just like last time, we are ready to push our app to our Power Platform environment!

Run npm run build followed by pac code push to push your Code App to your target Power Platform environment.

Image 27

Image 28

Additional Resources

Author

Written by Tim Hanewich, Sr. Solution Engineer at Microsoft