Step by Step Guide to Using PowerApps Component Framework (PCF) to Create Custom Controls

This blog will explain the step-by-step flow for creating a PCF component from scratch. We will first go through installing the required component for the building and development of the PCF component. After that, we will give you a detailed description of coding files and provide some information about some critical files in the development like typescript and manifest files. Once we deployed the component, then we will build the solution and deploy it to Dynamics 365 and configure it for an entity. 

Learn more about our Microsoft Dynamics 365 services

Prerequisites: 

We need Power Apps CLI, which allows us to create/update and deploy a custom control component. 

There are a few prerequisites before we go ahead. 

  1. We must have to install Node.js, and this will also install NPM. Node.js is the framework used to create server-side applications in JavaScript using an engine called V8, and NPM (Node.js Package Manager). This tool allows us to include, remove, and update the libraries in the project. 
  2. We must have .NET Framework version 4.6.2 as required for Power Apps CLI. To install, go to the Microsoft website, download it, or either use Visual Studio installer from there in individual Components and select .NET Framework 4.6.2 Targeting Pack & .NET Framework 4.6.2 SDK. 
  3. After that, we need to install Power Apps CLI to Download it Click Power Apps CLI. 
  4. We must have Visual Studio 2017 or a higher version. 

After completing the installation, we are now ready to create the project for PCF 

Steps to set up a PCF project. 

There are a few steps that we need to follow to create a PCF project. Let us go through it step by step. 

  1. Specify or create a working folder. In my case, it will be “C:\SamplePCF”. 
  2. After that, we need to use the oldest command prompt or use console emulators like CMDER. 
  3. Now we need to navigate to the folder that we specified in step 1. We will be using the “cd” command.
  4. After that, we will now run the “pac” command to create new PCF component projects. 

pac pcf init --namespace <namespace here> --name <Name of the code component> --template <component type>

This command will create a Power Apps Component Framework (PCF) project in our folder. The namespace parameter contains the value that you want. The name parameter will include the name that we give to the component. In component type, we will be giving the type for the component. So far, we have two template types available for the model-driven apps component: Field and Dataset, and only one supported for the canvas apps, which is Field.  

So, my command will look like this:

5. After that, we need to use the npm command to retrieve project dependencies. In the following command, we will be run to do this. 

 As of now, the project is ready. We can use any development tool for the implementation, but I will be using the Visual Studio Code. 

The below image will give you an overview after completing the above step: the file structure in the project folder. 

There will be a folder created in my case, "GridPCFComponent” which will be of the same name as we provide in the “pac" command. Now navigate inside the solder, and then you will see the below structure. 

Configure Manifest file 

Configure Manifest file is one of the most critical files in the whole project as it contains all the essential component definitions.  Let’s review some essential tags. 

Control TAG 

Attributes of control tags  

    • namespace: Provided in the “pac” command earlier. 
    • constructor: Provided in the “pac” command earlier. 
    • version: change the versioning if needed; elsewe can keep it to default. 
    • display-name-key: This will be the display name with no spaces for custom control. 
    • description-key: This description will be shown in D365 for custom control. 
    • control-type: We will keep the default value 

Property TAG

Attributes of property tags 

    • name: Provide the name that will be used for custom control. 
    • display-name-key: Provide the display name with no spaces for custom control. 
    • description-key: This description will be shown in D365 for custom control. 
    • of-type: If we are using single datatype, then there are some supported datatype that can be used for the of-type attribute. 

Valid values are: 

  1. TwoOptions 
  2. Whole.None 
  3. Currency 
  4. DateAndTime.DateAndTime 
  5. DateAndTime.DateOnly 
  6. SingleLine.Email 
  7. SingleLine.Phone 
  8. SingleLine.Text 
  9. SingleLine.TextArea 
  10. SingleLine.Ticker 
  11. SingleLine.URL 
  12. Decimal 
  13. Enum 
  14. FP 
  15. Multiple 
  16. Optionset 
    • of-type-group: if we are building control that will support multiple data-type, then we need to use the of-type-group attribute.  

Let us define a type-group: 

<type-group name="line"> 

<type>SingleLine.Email</type> 

<type>SingleLine.Phone</type> 

<type>SingleLine.Text</type> 

<type>SingleLine.URL</type> 

</type-group> 

DataSet TAG

 Attributes of data-set tags 

    • name: Provide the name of the data setused to get the value in custom control. 
    • display-name-key: Provide the display name with no spaces that will be set while importing in App. 
    • description-key: This description will be shown in D365 for custom control. 

Resources TAG 

Subtags of Resources tags 

  • code: Provide the relative path for typescript file which contains code for custom control 
  • css – Provide the CSS files that need to be added. 
  • resx – Provide the file path that contains static string contents that are needed for the control. 
  • img – Provide the images that are needed in the project. 

 

Typescript file 

This file is also created when we execute the “pac pacf” command, and it contains some signatures methods that are needed to implement. If you are not familiar with the typescript, you will be able to quickly grasp it, especially if you have JavaScript experience This file controls the lifecycle of the code component. Now let us go through each method. 

  • Init: This method in the lifecycle of the component used to initialize it. In this, we will initialize particular action or remote server calls for the component. There are four parameters for this method. Let's go through them one by one. 
Name  Type  Required  Description 
context  Context  yes  It contains the Input properties, which also contains the parameters, interface functions, and component metadata. 
state  Dictionary  no  This parameter saved the State from setConttrolState in the earlier session. 
notifyOutputChanged  function  no  This parameter gives the ides to the framework that it has a new output. 
container  HTMLDivElement  no  This parameter gives us the div element to render. 

  

  • updateView: This method will get triggered whenever any value is changed in the property bag. It can be field values, global values, datasets, component metadata, and so on. It contains a context parameter that includes the changed values that we have mapped to the name in the manifest and the utility functions. 
  • getOutputs: When a component receives new data, this method will get triggered. It returns the object, defined in the manifest file, either of type output or bound. 
  • destroy: This method will get triggered when a component gets removed from the DOM object. This method is used to get cleanup and release any memory that is still used by the component. 

 

Building and Testing the PCF control. 

We will give you a sample of a basic HTML table in the component.  Let us start by creating a CSS file. We will create a folder with the name of CSS and then create a file inside this folder with the name GridPCFComponent.css. As you can see in the below screenshot. 

After that, we need to update the ControlManifest.Input.xml to communicate the PCF control that the CSS file for this is component found at the following path. After adding, you will see the file like this. 

 

Starting coding 

We will start by creating a container for HTMLDivElement type into typescript file name index.ts. 

Now we will inject the div element with a constant element that will contain a predefined HTML table element with some static data. All this will be implemented in the init() method. Once we complete it, the method will look like this. 

Now we will run the “npm start” command so that we can start the application component testing in the PowerApps component framework Test Environment. 

The below screenshot depicts how the component will be displayed. 

 The image above indicates that the PowerApps component framework gives us an interface for debugging the real data and see what our component will respond to. 

Building a solution first, we need to create a Dynamics 365 environment profile that will be used to publish our component to the desired environment. To do this, we need to use the "pac auth” command. 

pac auth create --url <your organization url> 

Once you run this command, it will prompt the user in that particular environment to create the profile. 

 Once we provide valid credentials, the connection profile will be created for the environment. This will allow the command to display the Valid connection message below. 

After that, we will run the "pac pcf push" command to package the PCF component in a solution file(zip). This will allow it to be imported to the environment for which we have created the profile in the above image.  

pac pcf push --publisher-prefix <publisher prefix> 

The below image will show the output of the pac pcf push command. 

 Once the command runs successfully, it will import the environment's solution, as shown in the below image. 

Configure the PCF component in D365 

 Now we will create a solution and add an account entity to it for the demo purpose.  

As seen in the below image, there is a Controls tab where we need to click on the Add Control link. 

 After that, the popup will be prompt to select the control that we have created; in my case, it's “GridPCFComponent”. 

Once we select the component, we need to click on the “Add” button found at the bottom right corner. 

Once configured, we need to select the option on which we need to display the PCF component. In this demo, we will select “Web.” 

Now we have completed the configuration, and let’s load it in D365. 

Conclusion  

Conclusively, we went over the step-by-step process of installation to coding and then building and publishing the PCF component in Dynamics 365. I hope this will resolve any questions that occur in a new developer's mind, who is not familiar with the PCF component! As in this, we are giving an overview of creating a PCF component from scratch. 

For questions or insights, leave a comment below or reach out to us at [email protected]. We are always happy to help.  

 If you have any question or queries, do not hesitate to reach out to us! 

 

Leave a Reply

Your email address will not be published.