Multi-project templates

I had a comment left on my last post about enterprise templates, and it is a good time to discuss a new feature in our VSTemplate Wizard – Multi-Project Templates. The most common way of using the VSTemplate Wizard is to create a single project or project item. However, there may be times when you need to build an entire solution, with multiple projects that work together. And you can do that with Multi-Project Templates.


Suppose you need to build a solution with a WinForm project, a Web Service, and a Class Library, all within the same solution. With Visual Studio 2003, you need to add three separate projects to your solution, then write the code to wire the three projects together. To create a VS Template that wires the three of these together, you would first manually create the solution with these three projects, and wire up the code the way you want them to work. Then, one by one, use the Export Template tool to export the templates. Next, you would create a fourth template that links the three projects together, with an XML .vstemplate that looks much like this (I am writing this from home without the exact XML in front of me, so the exact XML may be a bit different):



    <ProjectLink Name=MyWinForm>WinForm.vstemplate</Project>

    <ProjectLink Name=MyClassLibrary>ClassLibrary.vstemplate</Project>

    <ProjectLink Name=MyWebService>WebService.vstemplate</Project>



The Template Wizard will first create a solution if the user selects to create a new solution, or will add the projects to the existing solution if the user so chooses, then walk through the XML and recursively call the Template Wizard to add each sub-project to the solution. Since you exported projects that all worked together when you first created the project, the WinForm project can call off to the Class Library, and call into the Web Service, which was installed into the correct place for use by a web server.


But you can also use Multi-Project Templates to organize your projects. Visual Studio supports what we are calling Solution Folders. These folders allow you to group projects together into folders. Just like you can create folders within a project to organize files, you can create folders within a solution to organize projects and files. You can construct XML in a Multi-Project Template to construct folders and organize projects. The XML for this looks like this:



    <Folder Name=UI Projects>

        <Project Name=MyWinForm1>WinForm.vstemplate</Project>

        <Project Name=MyWinForm2>WinForm.vstemplate</Project>


    <Folder Name=Class Libraries>

        <Project Name=MyClassLibrary>ClassLibrary.vstemplate</Project>


    <Folder Name=Web>

        <Folder Name=Services>

            <Project Name=MyWebService>WebService.vstemplate</Project>


        <Folder Name=WebApps>

            <Project Name=WebApplication>WebApplication.vstemplate</Project>






Not only can you use your own projects as sub-projects, but you can use a relative path to link to an existing project. For example, if you put your template in the correct location, you can link to existing project templates:



    <ProjectLink Name=MyWinForm>..\\WindowsApplication.vstemplate</Project>

    <ProjectLink Name=MyClassLibrary>..\\ClassLibrary.vstemplate</Project>




I also saw a comment saying that the images I uploaded from my last post were not showing. I uploaded them to a MSN Group, and it may be possible that the security settings were not set right, I will check into it and see what I can do

Comments (10)

  1. What are Project Templates

    Project templates are sample projects you can use to create new projects,…

  2. What are Project Templates

    Project templates are sample projects you can use to create new projects,…

  3. Charlie says:

    <Project Name="MyWinForm1">WinForm.vstemplate</Project>


    Can we change the project’s name"MyWinForm1" by our parameter ?

  4. Ravi Gyani says:

    <Project Name="MyWinForm1">WinForm.vstemplate</Project>


    How do we change the project’s name by our parameter? I tried using

    <Project Name="$safeitemname$.MyWinForm1">WinForm.vstemplate</Project> but it doesn’t work, cause there is no ReplaceParameters attribute here, Wat to do? Any solution?

  5. SirReal says:

    Hey Craig, I have a sticky Multi-Project Template (MPT) problem I’m sure many others are having.  I need to parameterize individual projects in a MPT; I want to substitute various custom parameters (like Namespace names in code), but it only seems to work for SPTs.

    I’ve found that you can add the "CustomParameters" element to a MPT VSTEMPLATE file, but that’s it.  These parms don’t make it into the "RunStarted.replacementsDictionary" (which they do in a SPT), and when added via Wizard code in "RunStarted", they aren’t substituted in a MPT Wizard as they are in a SPT Wizard.  

    It appears that the problem is, certain Wizard functions aren’t called during the execution for an MPT.  More accurately, it appears that there aren’t "escapes" by VS into your Wizard at t ehdesired moments, so for example, the end of the entire solution generation is only when "ProjectFinishedGenerating" is entered for example, and then the "Project" type passed in is null.  Seems like the "events" (in this case methods) are fired for each successive project in an MPT.

    Is there a way to circumvent this problem, is a fix coming (perhaps with more events and better control for MPTs), or is there a way to tap into the VS SDK during template generation?  

    It’d also be cool if there was a way to open a CSPROJ file, edit it, and replace what was deployed by the Wizard/VS (w/out VS giving you the "this thing was changed outside the env; reload?" msg); at least I could make necessary changes in the CSPROJ files themselves.  Again, this is probably something that having more "call-ins" at strategic points by Visual Studio during the generation might fix.

    Anyway thanks in advance!

  6. SirReal says:

    Sorry, meant to say:

    "Seems like the "events" (in this case methods) are NOT fired for each successive project in an MPT."

    3rd para, last sentence of previous post.

  7. SirReal says:

    Never mind, I stumbled my way through.  I can post the approach if anyone’s interesed; maybe I’ll post just to help out other poor saps like me w/ this problem!

    My problem now is how to:

    – Replace the project parameter "Assembly Name" so that I can generate the output assembly name fully qualified by the namespace (1 of our standards).

    – (All) My references don’t stick when I create a multi-project template; don’t know why or what the pattern is.  When a new project is generated upon VS/template execution, the projects all get new GUIDs, but the refs to them in other projects stay the old GUID.  It baffles me how some refs seem to stick though, even w/ this anomoly, but some do not.  If I modify the broken ones outside VS in the CSPROJ files to match the newly-gen’d project GUID, when I open the solutin back up, that fixes the refs.

  8. SirReal says:

    Man, takes a long time to get a reply on this blog!  Is it still monitored?

  9. ruisilva says:

    Hi, I’m building a software factory to automate the process of building the initial development solution using GAT/GAX.

    My kind of applications are module based so after creating the shell (for instance), I need to be able to add several modules.

    My Modules are composed by more than one project.

    I’ve managed to create the solution with the shell project, but I’m having problems trying to create a template that let me specify more than one project.

    Can someone help me please?

    Thanks in advance

    Rui Silva