Power Apps component framework – Beyond the boundary
Note: I am going to write some dark magic with PCF. It is not currently supported by Microsoft. These are my personal findings. Views are my own. But if you want to dismantle PCF and want to test the boundary, then read further.
Credit: Andrew Ly for his Number Button Selector control and feedback. Rami Mounla for proofread and correction on the content.
The source code can be downloaded at my github.
Intro
Microsoft has announced public preview of Power Apps component framework (PCF) and its Command Line Interface (CLI) in April 2019. Many developers, including me, have been anticipating this new feature. I find the documentation useful. A few people has started building and sharing new controls. No disappointment from this beautiful community.
Absolute need
TypeScript. Although I am not absolutely convinced the preference of TypeScript to JavaScript (by the way, JavaScript is my favorite), it is the only choice we have right now. However, Andrew Ly has pointed out essential areas where TypeScript really shines, such as strong typing, OO concepts thus bringing the gap between JavaScript and OO languages like C#/Java.
Beyond absolute need
There is no doubt you can build any control following Microsoft documentation. But, I don’t want to stop here. I want to push its boundary and limits. So far, I recommend adding 3 more types of knowledge.
2. React for UI components
When the sample from Microsoft documentation shows building the UI using TypeScript, it reminds me of my student days when I had an assignment task asking to build a UI in C++. I could not force myself to create UI in C++ program.
There are many libraries to choose. However, you can find react and react-dom packges under node_modules after executing “npm install” at step 4.. So, React is the choice. You can also use JSX which makes life easier when building DOMs.
2. NPM for packages
The motto of “Why reinvent the wheel?”. NPM hosts thousands of free packages. Most of the well-established libraries are now distributing over NPM. It is like NuGet for JavaScript. Now, you may ask “why not reference the CDN?”. Well, that brings me to next one.
3. Webpack for bundling
A module bundler. It will bundle all your resources, and packages from NPM, as a single package. Referring to CDN will only work when there is connectivity (or it is dependency). If we want to use it offline in mobile devices, we may encounter some issues.
The good news is that you don’t need to worry about NPM and webpack. PCF CLI handles them gracefully in most cases. I did however notice that there are some issues with adding large NPM packages (>500kb). The bundling tends to behave weirdly.
Create a new PCF Project
You use “pac pcf init” command to create a new PCF project. It will pre-create ControlManifest.Input.xml and index.ts.
Create a react component
Let’s create DemoComponent.tsx and write some scripts. The sample source can be found at my github repository.
Run ‘npm run build’ command. You will get an error saying ‘–jsx’ is not set. To resolve this, you need to add “jsx”: “react” in tsconfig.json located under your root folder. After this, you will be able to run ‘npm run build’ command without any issue.
Now, let’s render DemoControl in index.ts. You will see an error and you will not be able to run ‘npm run build’ command.
Now, let’s change index.ts to index.tsx and you will see the error is gone from IDE. You also need to update the code file extension in ControlManifest.Input.xml.
But, when you run ‘npm run build’, you run into another issue saying ‘Code file needs to be a typescript (.ts)’.
Looking at the error, it comes from pcf-scripts module which is located under node_modules folder in your PCF project.
A quick search using the error message brings me to line 84 at controlcontext.js under pcf-scripts module.
You simply need to update ‘if’ statement to allow .tsx extension and save it. And Magic happens.
Now, if you run ‘npm run build’, it will succeed. Next, it’s time to execute ‘npm start’. When the browser is launched, you will see the error ‘React’ is not defined.
To address this error, open index.html under your pcf root folder\node_modules\pcf-start and add following two script tags.
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
As soon as you save your changes, your react component will be rendered.
Final word
Even though you can deploy React control to model-driven app, this approach is not currently supported by Microsoft. I am sharing this only for experimental purposes and showing how PCF can be extended using modern libraries. I am not a pro-React developer nor a TypeScript developer, therefore, I may have approached this the wrong way. If you have any comments, please share them below.
Hi Aung,
This is suresh, I am trying to use your PCF Contorl ( Dataset Visualization) for a demo purpose. I have created solution and deployed in CRM successfully. I am using the control on the connections subgrid but the control does not load tried different browser but no luck.
Can you please give some instruction on how to use this control. This is a bit urgent and important your help will be appreciated