Package a code component

This article describes how to import code components into Microsoft Dataverse. After implementing the code components using Microsoft Power Platform CLI, the next step is to bundle all the code component elements into a solution file and import the solution file into Dataverse 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 pac solution init command. The solution project is used for bundling the code component into a solution zip file that is used for importing into Dataverse.

    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 pac solution add-reference command. 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:restore
    
    msbuild
    

    Or if you have installed the .NET SDK, version >= 6 :

    dotnet build
    

    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 Dataverse 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 Microsoft Power Platform CLI by connecting to the Dataverse environment and then pushing the updated components.

Follow the steps below to create the authentication profile, connect to Dataverse, and push the updated components.

  1. Create your authentication profile using the pac auth create 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 pac auth list command:

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

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

    pac org who 
    
  5. To delete a particular authentication profile, use the pac auth delete 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 pac auth clear command.. 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 Dataverse 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 pac pcf push 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.

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 column or table in model-driven apps
Add components to a canvas app
Power Apps component framework API reference
Power Apps component framework overview
Microsoft Power Platform CLI Command Groups