Share via


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):

 

<MutliProjects>

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

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

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

</MutliProjects>

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:

 

<MutliProjects>

    <Folder Name="UI Projects">

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

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

    </Folder>

    <Folder Name="Class Libraries">

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

    </Folder>

    <Folder Name="Web">

        <Folder Name="Services">

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

        </Folder>

        <Folder Name="WebApps">

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

        </Folder>

    </Folder>

</MutliProjects>

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:

 

<MutliProjects>

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

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

</MutliProjects>

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

  • Anonymous
    August 17, 2005
    What are Project Templates
    Project templates are sample projects you can use to create new projects,...

  • Anonymous
    August 17, 2005
    What are Project Templates
    Project templates are sample projects you can use to create new projects,...

  • Anonymous
    December 19, 2005
    <Project Name="MyWinForm1">WinForm.vstemplate</Project>
    ------------------------------------
    Can we change the project's name"MyWinForm1" by our parameter ?

  • Anonymous
    July 18, 2006
    <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?

  • Anonymous
    April 13, 2008
    PingBack from http://www.autoeverything.com.cn/2008/04/14/%e4%bd%a0%e6%98%af%e5%90%a6%e7%9f%a5%e9%81%93-visualstudio%e4%b8%ad%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8%e5%b7%a5%e7%a8%8b%e6%a8%a1%e6%9d%bf/

  • Anonymous
    May 12, 2008
    The comment has been removed

  • Anonymous
    May 12, 2008
    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.

  • Anonymous
    May 14, 2008
    The comment has been removed

  • Anonymous
    May 14, 2008
    Man, takes a long time to get a reply on this blog!  Is it still monitored?

  • Anonymous
    June 17, 2009
    The comment has been removed