Skip to content

How to create a project template

RandomlyKnighted edited this page Dec 18, 2014 · 30 revisions

In this article, I will show you how to create a project template.

1. Let's Get Started

First, fork SideWaffle if you haven't already and open SideWaffle.sln in Visual Studio

Fork the project

2. Add the project

Add the source project to the solution

3. Create a new Template Reference

Right click your VSIX project and select Add->Template Reference

4. Add the SideWaffle Project Template Files

Right click your source project and select Add New Item->Extensibility\SideWaffle Project Template Files as shown in the image below. You should always add the item to the root directory of your project in order for SideWaffle to create the files in the correct location. This item adds the following files to the target project to turn it into a template:

  • _preprocess.xml - This file is used to perform file replacements during the template packing process and to override the path where the template is shown in the New Project/Item dialog; This file Must be in the root folder, and must define the Path attribute on TemplateInfo
  • _Definitions\_project.vstemplate.xml - This file contains metadata regarding your template. For example the name, description, logo, etc are all found in this file. This file also has a pointer to the project file that you are creating a template out of.

New project dialog with correct template selected

5. Updating the _preprocess.xml file

You'll need to update _preprocess.xml so that it replaces your namespace values to use $safeprojectname$ when the template is created and so that you define where the template should be shown in the New Project dialog. The XML below is an example of a very common and simple replacement of the _preprocess.xml file.

<?xml version="1.0" encoding="utf-8" ?>
<Preprocess>
  <TemplateInfo Path="CSharp\Web\Custom\Subcategory"/>
  <Replacements Include="*.*" Exclude="*.vstemplate;*.csproj*.jpg;*.png;*.ico;_preprocess.xml">
    <add key="_SampleProjRef" value="$safeprojectname$"/>
  </Replacements>
</Preprocess>

Below is another example of the _preprocess.xml file using a more complex replacement.

<?xml version="1.0" encoding="utf-8" ?>
<Preprocess>
  <!--
  You can specify the path where this should show up in the
  Add New Project / Add New Item dialog by setting the value below
  -->
  <TemplateInfo Path="CSharp\Web\SideWaffle"/>
  <Replacements Include="*.*" Exclude="*.vstemplate;*.fsproj;*.vbproj;*.jpg;*.png;*.ico;_preprocess.xml;_project.vstemplate.xml">
    <add key="Nancy.CSharp.AspNetHost" value="$safeprojectname$"/>
    <!--targetFramework="4.0"-->
    <add key="targetFramework=&quot;4.0&quot;" value="targetFramework=&quot;$targetframeworkversion$&quot;"/>
    <!--<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>-->
    <add key="&lt;TargetFrameworkVersion&gt;v4.0&lt;/TargetFrameworkVersion&gt;" 
         value="&lt;TargetFrameworkVersion&gt;v$targetframeworkversion$&lt;/TargetFrameworkVersion&gt;"/>
  </Replacements>
</Preprocess>

5. Updating the _Definitions_project.vstemplate.xml file

You'll need to update the _Definitions\_project.vstemplate.xml file and ensure that the correct content for the File attribute on the Project element was generated. Below is an example of a complete file.

Note: Make sure to set the Path attribute on TemplateInfo here otherwise the template will never be shown

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
  <TemplateData>
    <Name>Sample proj from ref</Name>
    <Description>Sample proj from ref</Description>
    <DefaultName>MyProjApp</DefaultName>
    
    <ProjectType>CSharp</ProjectType>
    <ProjectSubType></ProjectSubType>
    <SortOrder>1000</SortOrder>
    <CreateNewFolder>true</CreateNewFolder>    
    <ProvideDefaultName>true</ProvideDefaultName>
    <LocationField>Enabled</LocationField>
    <EnableLocationBrowseButton>true</EnableLocationBrowseButton>
    <Icon>icon.png</Icon>
    
    <!-- Indicates how many parent folders this item template should appear in -->
    <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp>
  </TemplateData>
  <TemplateContent>
    <Project TargetFileName="$projectname$.csproj" File="_SampleProjRef.csproj" ReplaceParameters="true">      
    </Project>
  </TemplateContent>
</VSTemplate>

After you have made these changes you can party on your project and then when you build the TemplatePack project the project template will be included automatically.

You can find a sample of this in the SideWaffle repo as the _SampleProjRef project.

If you have any trouble following the instructions found in this article or are unsure about something in your project please create an issue and we will be happy to assist you with your project.

Supplemental Material

Below is a 5 minute video demonstrating how to create a new project template with SideWaffle.

New project dialog with correct template selected - https://www.youtube.com/watch?v=z33jOo75CH4&feature=youtu.be

https://www.youtube.com/watch?v=z33jOo75CH4