Redaguoti

Package a code component

Learn how to package a code component in a solution file and import it into Microsoft Dataverse so it's available at runtime. You also learn how to connect to an environment, deploy updates with Microsoft Power Platform CLI, and remove components from a solution.

Process

To create and import a solution file:

  1. Create a new folder inside the sample component folder and name it Solutions (or any name you choose) by using the command mkdir Solutions. Navigate into the directory by using the command cd Solutions.

  2. Create a new solutions project by using the pac solution init command. Use the solution project for bundling the code component into a solution ZIP file that you import 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. After you create the new solution project, set the Solutions folder as the location for the created sample component. Add the reference by using the pac solution add-reference command. This reference informs the solution project about which code components to add 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 by 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 flag only the first time you build the solution project. For every build after that, run the command msbuild.

    msbuild /t:restore
    
    msbuild
    

    Or if you have installed the .NET SDK, version 6 or later:

    dotnet build
    

    Tip

    • If msbuild 15.9.* isn't 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. Building the solution in release configuration generates a managed solution package. You can override these settings by specifying the SolutionPackageType property in the cdsproj file.
    • 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 aren't the same.
  5. After the build succeeds, find the generated solution files inside the \bin\debug\ folder.

  6. Import the solution into Dataverse manually by using the web portal or automatically by using the Microsoft Power Platform Build Tools.

Connect 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 in the following list to create the authentication profile, connect to Dataverse, and push the updated components.

  1. Create your authentication profile by using the pac auth create command.

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

     pac auth list 
    
  3. Switch between previously created authentication profiles by using the pac auth select command.

     pac auth select --index <index of the active profile>
    
  4. Get basic information about the environment by using the pac org who command. The connection uses the default authentication profile.

    pac org who 
    
  5. Delete a particular authentication profile by using the pac auth delete command with the pac auth delete --index <index of the profile> syntax.

  6. Clear all authentication profiles from your local machine by using 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.

Deploy code components

After you create an authentication profile, 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 doesn't require that you build your solution (cdsproj) to import the code component.

To use the push capability, follow these steps:

  1. Ensure that you have a valid authentication profile.

  2. Go 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 where you include the components.

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's 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) by 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