Skip to main content

Exploring CDS Project type

By Betim Beja ·

The CDS Project type was introduced alongside the PCF Project type to simplify the ALM (Application Lifecycle Management) for PCF Components. In this article, we will focus on the CDS Project and how it can be configured to handle packaging of Microsoft Dataverse solutions.

To create a CDS Project from a dataverse solution you need to have the Power Platform CLI installed. The the following command will connect to your environment and create an unpacked solution project:

terminal
pac solution clone --name AlbanianXrmStyledSwitch

The result of the previous command is a new folder in your working directory using the UniqueName of the solution. Inside this folder you get the CDS Project a .gitignore and a src folder. Inside the src folder you get the extracted solution with all the artifacts (the result of SolutionPackager extract action).

We will focus on the CDS Project definition and customizations. The file you get as a result of the previous clone command is the following:

AlbanianXrmStyledSwitch.cdsproj
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <PowerAppsTargetsPath>$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\PowerApps</PowerAppsTargetsPath>  
  </PropertyGroup>
  
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
  <Import 
    Project="$(PowerAppsTargetsPath)\Microsoft.PowerApps.VisualStudio.Solution.props" 
    Condition="Exists('$(PowerAppsTargetsPath)\Microsoft.PowerApps.VisualStudio.Solution.props')"/>

  <PropertyGroup>
    <ProjectGuid>3c1a3f4a-e44f-436c-ab95-16f49b1b3907</ProjectGuid>
    <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
    <!--Remove TargetFramework when this is available in 16.1-->
    <TargetFramework>net462</TargetFramework>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
    <SolutionRootPath>src</SolutionRootPath>
  </PropertyGroup>

  <!-- Solution Packager overrides, un-comment to use: SolutionPackagerType (Managed, Unmanaged, Both)
  <PropertyGroup>
    <SolutionPackageType>Managed</SolutionPackageType>
  </PropertyGroup>
  -->
  
  <ItemGroup>
    <PackageReference Include="Microsoft.PowerApps.MSBuild.Solution" Version="1.*"/>
    <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" />
  </ItemGroup>
  
  <ItemGroup>
    <ExcludeDirectories Include="$(MSBuildThisFileDirectory)\.gitignore"/>
    <ExcludeDirectories Include="$(MSBuildThisFileDirectory)\bin\**"/>
    <ExcludeDirectories Include="$(MSBuildThisFileDirectory)\obj\**"/>
    <ExcludeDirectories Include="$(MSBuildThisFileDirectory)\*.cdsproj"/>
    <ExcludeDirectories Include="$(MSBuildThisFileDirectory)\*.cdsproj.user"/>
    <ExcludeDirectories Include="$(MSBuildThisFileDirectory)\*.sln"/>
  </ItemGroup>
  
  <ItemGroup>
    <None Include="$(MSBuildThisFileDirectory)\**" Exclude="@(ExcludeDirectories)" />
    <Content Include="$(SolutionPackageZipFilePath)">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" />
  <Import 
    Project="$(PowerAppsTargetsPath)\Microsoft.PowerApps.VisualStudio.Solution.targets" 
    Condition="Exists('$(PowerAppsTargetsPath)\Microsoft.PowerApps.VisualStudio.Solution.targets')"/>
</Project>

The SolutionRootPath controls the relative path where the extracted solution artifacts can be found. The default value for this is src, and we need to change it if we want to use a map file because when the solution packager is called from MSBuild the files are actually copied to an intermediate location determined by the following property IntermediateOutputPath, which defaults to obj\Release or obj\Debug, and this means that to keep the same depth on relative paths we must increase the depth of the source folder.

To specify a Map file the following property can be used SolutionPackageMapFilePath, as in the example that follows

MapFile.cdsproj
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- This part of the code was removed for simplicity -->
  <PropertyGroup>
    <SolutionRootPath>src\solution\AlbanianXrmStyledSwitch</SolutionRootPath>
    <SolutionPackageType>Managed</SolutionPackageType>
    <SolutionPackageMapFilePath>$(MSBuildThisFileDirectory)Map.xml</SolutionPackageMapFilePath>
  </PropertyGroup>
 <!-- This part of the code was removed for simplicity -->
</Project>

We are using a Map.xml file that is in the same directory as our CDS project, and to create a full path to that file we can use the $(MSBuildThisFileDirectory) variable. We changed other two properties as well. We specified the SolutionPackageType to be Managed and we changed the SolutionRootPath to add two levels of depth so that it is aligned with the IntermediateOutputPath depth that was mentioned earlier.

Other properties you can configure are:

  • OutputPath = bin\Release or bin\Debug - the path where the solution zip file will be written to
  • SolutionPackageErrorLevel = Info - Can be one of the following Off, Error, Warning, Info, Verbose
  • SolutionPackageEnableLocalization = false - Extract or merge all string resources into .resx files
  • SolutionPackagerWorkingDirectory = $(IntermediateOutputPath)
  • SolutionPackagerMetadataWorkingDirectory = $(SolutionPackagerWorkingDirectory)Metadata
  • SolutionPackagerLocalizationWorkingDirectory = $(SolutionPackagerWorkingDirectory)LocExt
  • SolutionPackageLogFilePath = $(IntermediateOutputPath)SolutionPackager.log - A path and name to a log file. If the file already exists, new logging information is appended to the file.
  • SolutionPackageZipFilePath = $(OutputPath)$(MSBuildProjectName).zip - The path and name of a solution .zip file
  • SolutionPackageDefaultLocale = en-US - Used only for PowerAppsRefreshLoc target which depends on ProcessCdsProjectReferencesOutputs
  • SolutionPackageLocalTemplate = Auto