2

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:

<ItemGroup>
<ProjectReference Include="path\xyz.vcxproj">
<Project>{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}</Project>
</ProjectReference>

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.

user3358771
  • 297
  • 2
  • 10

2 Answers2

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.

Steps

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

From this

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

To this

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

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

  • 1
    Just make sure the tag of and inside template data does not have an addentilanal prefix or postfix it just should be just $safeprojectname$ – Ali Besharati Apr 28 '22 at 04:03
1

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_")

 <ProjectGuid>{$ext_guid1$}</ProjectGuid>

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

<ItemGroup>
  <ProjectReference Include="..\$ext_safeprojectname$.Data\$ext_safeprojectname$.Data.vbproj">
    <Project>{$ext_guid1$}</Project>
    <Name>$ext_safeprojectname$.Data</Name>
  </ProjectReference>
</ItemGroup>

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

skaddy
  • 109
  • 6