Adding references in Visual Studio project template?

爷,独闯天下 提交于 2021-02-19 05:55:07


I'm creating a multi-project template for VS2015 where one of the created projects references the other. How do I add the reference using the template?

If I add the reference using the VS GUI it will add the following to the .vcxproj file:

<ProjectReference Include="path\xyz.vcxproj">

And the GUID is a valid one, since VS knows the GUID of the referenced project. When I create the new projects from template I don't know what GUID will be chosen for the newly created project, so I can't add a valid GUID by using the template parameters. I can easily add the Include="..." part correctly, but the GUID I don't have.

I couldn't find much info on the <ProjectReference> tag either, but it seems that a valid GUID is required, leaving the <Project> tag out causes the reference to not work, and same happens if I use all zeros GUID.


You can generate the GUIDs in the solution template and pass them to the projects. Therefor you need the parameter CopyParameters in the ProjectTemplateLink in Solution Template. Example:

<ProjectTemplateLink ProjectName="$safeprojectname$.Data"  CopyParameters="true">

Your project template has to replace parameters

<Project TargetFileName="Data.vbproj" File="Data.vbproj" ReplaceParameters="true">

Now you can access the parameters from solution template inside the project file and set the external guid parameter (note the acces to the external parameter have a "$ext_")


For the project with the reference you use the ProjectReference inside ItemGroup

  <ProjectReference Include="..\$ext_safeprojectname$.Data\$ext_safeprojectname$.Data.vbproj">

It worked for me using Visual Studio 2017 with .NET Framework 4.5.2


In my case I have 2 project references in the main project. It worked for me in Visual Studio 2019, Net Core 3. as @skaddy answer, but not after several Visual Studio restarts and finally machine restart (that sucks).

The difference is that I did not care about the guid I just let Visual Studio care about that.


  1. In the solution template, the Root.vstemplate I set CopyParameters="true"
    <ProjectTemplateLink ProjectName="$safeprojectname$.API" CopyParameters="true">
  1. I ensure that ReplaceParameters="true" was there in the main project template ( it was since the beginning).
        <Project TargetFileName="APITemplate.csproj" File="APITemplate.csproj" ReplaceParameters="true">
  1. In the main .csproj file I look at the references and change them with the parameters

From this

        <ProjectReference Include="..\Data\Data.csproj"/>
        <ProjectReference Include="..\Service\Service.csproj"/>

To this

        <ProjectReference Include="..\$ext_safeprojectname$.Data\$ext_safeprojectname$.Data.csproj"/>
        <ProjectReference Include="..\$ext_safeprojectname$.Service\$ext_safeprojectname$.Service.csproj"/>

Again, for me the parameters just works after machine restart.