Package a code component

This article describes how to import code components into Common Data Service. After implementing the code components using the Power Apps CLI, the next step is to bundle all the code component elements into a solution file and import the solution file into Common Data Service so that you can see the code components in runtime.

To create and import a solution file:

  1. Create a new folder inside the sample component folder and name it as Solutions (or any name of your choice) using the command mkdir Solutions. Navigate into the directory using the command cd Solutions.

  2. Create a new solutions project using the following command. The solution project is used for bundling the code component into a solution zip file that is used for importing into Common Data Service.

    pac solution init --publisher-name developer --publisher-prefix dev
    

    Note

    The publisher-name and publisher-prefix values must be unique to your environment.

  3. Once the new solution project is created, refer the Solutions folder to the location where the created sample component is located. You can add the reference using the command shown below. This reference informs the solution project about which code components should be added during the build. You can add references to multiple components in a single solution project.

     pac solution add-reference --path c:\downloads\mysamplecomponent
    
  4. To generate a zip file from the solution project, go into your solution project directory and build the project using the following command. This command uses MSBuild to build the solution project by pulling down the NuGet dependencies as part of the restore. Use the /restore only for the first time when the solution project is built. For every build after that, you can run the command msbuild.

    msbuild /t:build /restore
    

    Tip

    • If msbuild 15.9.* is not in the path, open Developer Command Prompt for VS 2017 to run the msbuild commands.
    • Building the solution in the debug configuration generates an unmanaged solution package. A managed solution package is generated by building the solution in release configuration. These settings can be overridden by specifying the SolutionPackageType property in the cdsproj file.
    • You can set the msbuild configuration to Release to issue a production build. Example: msbuild /p:configuration=Release
    • If you encounter an error that says Ambiguous project name when running the msbuild command on your solution, ensure that your solution name and project name are not the same.
  5. The generated solution files are located inside the \bin\debug\ folder after the build is successful.

  6. Manually import the solution into Common Data Service using the web portal or automatically using the Microsoft Power Platform Build Tools.

Connecting to your environment

You can deploy the code components directly from the Power Apps CLI by connecting to the Common Data Service environment and then pushing the updated components.

Follow the steps below to create the authentication profile, connect to Common Data Service, and push the updated components.

  1. Create your authentication profile using the command:

    pac auth create --url https://xyz.crm.dynamics.com 
    
  2. If you have previously created an authentication profile, you can view all the existing profiles using the command:

     pac auth list 
    
  3. To switch between the previously created authentication profiles, use the command:

     pac auth select --index <index of the active profile>
    
  4. To get the basic information about the environment, use the following command. The connection will be made using the default authentication profile.

    pac org who 
    
  5. To delete a particular authentication profile, use the command pac auth delete --index <index of the profile>.

  6. If you want to clear all the authentication profiles from your local machine, use the command pac auth clear. This action is irreversible because it completely deletes the authprofile.json file and token cache file from your local machine.

Deploying code components

After you have successfully created an authentication profile, you can start pushing the code components to the Common Data Service instance with all the latest changes.

The push capability speeds up the inner-developer cycle development because it bypasses the code component versioning requirements and does not require that you build your solution (cdsproj) to import the code component.

To use the push capability, do the following:

  1. Ensure that you have a valid authentication profile created.

  2. Navigate to the directory where the sample component file is located.

  3. Run the command.

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

    Note

    The publisher prefix that you use with the push command should match the publisher prefix of your solution in which the components will be included.

Create a solution project based on an existing solution in Common Data Service

To create a solution project based on an existing solution in Common Data Service, run the command pac solution clone. To do so:

  1. Ensure that you have a valid authentication profile created.

  2. Run the command

    pac solution clone –-name(-n) <name of the solution to be exported> --targetversion(-v) <version of your solution> --include(-i) <settings that should be included>
    

    Note

    Settings Values: autonumbering, calendar, customization, emailtracking, externalapplications, general, isvconfig, marketing, outlooksynchronization, relationshiproles, sales.

More information: Settings options

Create a plug-in project and add a reference to it in your solution

Note

The plugin command is in public preview and Power Apps CLI now supports creating a plug-in project and packaging it in a solution by adding a reference to the plug-in project. The pac plugin init command creates the template files (csproj, Plugin.cs & ServiceHelper.cs) in the directory. To do so:

  1. Ensure that you have a valid authentication profile created.

  2. Navigate to the root directory where you want the project to be created.

  3. Run the command

    pac auth create –url <https://xyz.crm.dynamics.com>
    
  4. Run the command to create the plug-in project

    pac plugin init
    
  5. Add a reference to your solution project using the following command so that the plug-in project gets built when the solution is built.

    pac solution add-reference –path <path to your plugin project>
    
  6. Run the command to build the solution and the referenced plug-in.

    msbuild
    

Remove components from a solution

If you want to remove a code component from a solution file:

  1. Edit the cdsproj file in the solution project directory and remove the references to the component. Here is an example of a component reference:

    <ItemGroup>
        <Projectreference Include="..\pcf_component\pcf_component.pcfproj">
          <Project>0481bd83-ffb0-4b70-b526-e0b3dd63e7ef</Project>
          <Name>pcf_component </Name>
          <Targets>Build</Targets>
          <referenceOutputAssembly>false</referenceOutputAssembly>
          <OutputItemType>Content</OutputItemType>
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Projectreference>
    </ItemGroup>
    
  2. Perform a rebuild (or clean) using the following command:

    msbuild /t:rebuild
    

See also

Add code components to a field or entity in model-driven apps
Add components to a canvas app
Power Apps component framework API reference
Power Apps component framework overview