As we discussed in some of my previous blogs, Power Apps Component Framework (PCF) based components within Dynamics 365 is the new talk of the town. To get this going, we need some tools for our help like Command Line Interface (CLI), Visual Studio Code, Code editor of your choice and so on. In this blog we are going to discuss more on CLI tool which will come handy in creating the project using a template, building and debugging the project and also during the packaging of our components. We will be taking this step by step as I was learning using the same along with the commands for you to keep them handy for future use as well.
If you already don’t have the Command Line Interface (CLI) please download the executable from this link. Its pretty easy and straight forward to install the same. Once its installed using the below command from the developer’s command prompt for VS we can install the latest version of CLI. (Note that this latest version installation is not required if you already have the VS Code extension)
pac install latest
From here we have to use couple of commands to create the initial files using a template. Below command can be used to initialize a component using a field or dataset template. In the below code replace “Name_of_your_namespace” with whatever is the name of your namespace and similarly replace “Name_of_your_component” with the name you intent to give to the component you are going to create. For the template part either give field or dataset as per your choice for the component you intend to make.
pac pcf init --namespace <<Name_of_your_namespace>> --name <<Name_of_your_component>> --template <<field/dataset>>
A prerequisite for creating a code component is to install node package manager (npm) to manage the dependencies. After initializing your component using the previous command we will have a file package.json which is configured with all the dependencies for our components and several commands that will be used during the development phase. Now its our turn to get the npm installed using the below command. This will make sure all the libraries required for our component is installed properly.
For verifying the component which we are developing using the build command from npm as follows. This will validate our manifest, runs the TypeScript compiler (called transpiler?) and we will get to know about any problems
npm run build
We do have a testing mechanism along with this which can be used to test this locally without deploying it to any of our environments. For that purpose, we can use the below command.
The following command can be used to build and push and the latest version of our component to currently configured Dataverse development environment.
pac pcf push --publisher-prefix <<name_of_publisher>>
When we have to package our component as part of a Power Apps solution for deployment into an environment we have to use msbuild command. One example is as follows, where name_of_publisher, prefix_for_publisher and folder_where_solution_needs_to_be_copied needs to be replaced with the respective values from your environment and local system respectively.
pac solution init --publisher-name <<name_of_publisher>> --publisher-prefix <<prefix_for_publisher>> --outputDirectory <<folder_where_solution_needs_to_be_copied>>
Now we have to add our solution with the component reference using below code by replacing path_to_your_project with the folder path where your project is in the local machine.
pac solution add-reference --path <path_to_your_project>
Next step would be to generate the solution files using the msbuild command.
msbuild /t:build /restore
In case you are getting any error while running the above command, try to execute the same command from visual studio command prompt. After the first build we can just run the msbuild command to regenerate the solution files. We don’t have to mention “/restore” or even “/t:build”. If everything goes well as planned, then we should be able to see the solution file under the folder “\bin\debug” or “\bin\release”. What would be left out now is to import the solution file to the D365 environment and then use that in our forms or views as required.
One main thing which I faced during my initial PCF work is that, when I was trying to build (not with the restore) I was getting an error regarding the path not found which felt to be little strange. Two things I tried and it worked. But I am not sure what is the real reason for that to work.
- First thing was that the path of my folders where all these commands were running was little lengthy. I tried to keep a base folder in my C drive thus keeping the path as small as possible.
- I have made sure to keep my solution folder where the build is running in the same root folder instead of any other folder inside my component project.
Through this blog, I was trying to help you with the step by step processes (from a CLI perspective) to create a code component. In between these steps after installing the dependencies, we do get a step for developing and after that testing the component which we created. Will try to come up with that in another blog. Till then stay tuned. Thanks for reading and I really hope it was helpful.
One thought on “Power Apps Command Line Interface (CLI)”