Skip to content

How to create a project template

Sayed Ibrahim Hashimi edited this page Jan 3, 2014 · 30 revisions

This doc covers the latest method to create a project template. The experience to create a project template has significantly changed in December 2013.

Video on how to create project templates

Watch this 5 minute video on how to create project templates with SideWaffle.

New project dialog with correct template selected

Docs

Steps to add a new project template to SideWaffle

  1. Fork SideWaffle if you haven't already and open SideWaffle.sln
  2. Copy the project that you want to use as a template under the side-waffle folder
  3. Add the project to the solution
  4. In the TemplatePack project add a reference the project you just added
  5. Add the following files to the target project to turn it into a template
  6. _preprocess.xml (_Must be in the root folder, and must define the Path attribute on TemplateInfo
  7. _Definitions\_project.vstemplate.xml

For the steps where you add the _preprocess.xml and _Definitions\_project.vstemplate.xml files there is an item template that you can use to create these files for you. To use it right click on the the project you want to turn into a project template and select Add New item and then select the SideWaffle Project Template Files template. You can see this template in the image shown below. (Note: this requires that you have SideWaffle installed)

New project dialog with correct template selected

You should always add the item to the root directory of your project. It will create the files in the correct location. Here is more info on those files.

  • _preprocess.xml - This file is used for the following; 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.
  • _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.

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. Below is an example of a complete 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>

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.