A while ago i wanted to play with some new frameworks, but it took me some time to get the Visual Studio project setup correct and as minimal as possible. So i thought to myself, what if i had a project template so that i won’t have to do this over and over again. I decided to create a project template, and because i learned a couple of things i will describe how i did it in this post.
Visual Studio 2015 supports exporting a project to a template, but if you use the out of the box template exporting functionality of Visual Studio you will have to follow some extra steps to make the template really work. For instance when i tried to use the project template to start a project straight after exporting the template i got a project with no files in it. Also, after fixing that problem, i found out that you need to clean your template, because if you don’t your project will initialize with some preset settings every time you use the template (like assembly info).
Microsoft MSDN – Creating Custom Project and ItemTemplates in Visual Studio
Steps for creating a Visual Studio 2015 project template
1. Create and setup a new project
First off, you create a new project of the desired type, using the
Visual Studio > File > New > Project menu. The project type you pick at this point will be the base for your template. After you have created the new project you can setup the project so that it has all the components that you want your template to contain. At this point i guess you should go for a minimal implementation of components, so that the template will end up having little features that you need delete after starting new projects with it.
2. Export the project to a template
To create a project template in Visual Studio 2015 choose the
Visual Studio > File > Export template... menu. The following screen will appear:
This screen states you want to start a new project template and selects your current visual studio project by default, in case of this example the project name was TemplateApplication. Note that eventually this project name will not be visible to users that use the template we are now creating, it will be replaced by whatever name they choose for the new project (based on this new template). Anyway, everything is ok so press Next and the following screen will appear:
This screen contains the basic meta data setup for your new project template.
Template name contains the name under which the template will be visible in the Installed Templates when you choose New Project in Visual Studio.
Template description should contain a descriptive text explaining what the goal of the template is (tip: include version number in description).
Icon image contains the icon/image that is showed before the template name in the Installed Templates when you choose New Project. Because we are going to clean the template before using it we will uncheck the
Automatically import the template into Visual Studio. This way the project is not visible in the Installed Templates until it is really ready for use.
Setup the values correct and press Finish. An explorer window will open at the location where the template is created which wel will use in step 3.
3. Clean up the template
The explorer window in step 2 opened at
<some-folder>\Visual Studio 2015\My Exported Templates and a zip file containing the new project template is present. The zip file will be named after the Template name that you used in step 2.
Extract the zip file
To start cleaning up the template you first need to extract the zip file to a directory. After extracting the zip file the directory will contain the ‘raw’ template that we just created.
In the template directory there is a
MyTemplate.vstemplate file, this file contains the template settings and we will edit this file to clean up the template. You can rename this file if you want, but you should not change the type of the file (.vstemplate).
Edit the MyTemplate.vstemplate file
Open the MyTemplate.vstemplate file in any text editor so we can edit it. You will see the content type of this file is xml. Basically there are 2 sections in this xml,
<TemplateData> section the meta data of the template is stored and in the
<TemplateContent> the project files (to copy/use) are listed.
<TemplateData> section does not need to be edited, but for some project types (like DNX) you should add the following element
<CreateInPlace>true</CreateInPlace>. Microsoft documentation says it’s default value is true, but if this setting is not present and set to true you have a good chance your template will not copy the project files in the
<TemplateContent> section. So if you have no file structure in your project after using this new template, try to add this element.
<TemplateContent> section, we can clean up the files that are copied to the project that the template will create. In visual studio projects, most of the time you will have a properties folder which contains information about the project (assembly). This file is autogenerated when it is missing and that is exactly the behaviour we want in a project template. So if there is a folder with name Properties in the root of the project section of
<TemplateContent>, delete it and all of it’s sub items. ( Look for
<Folder Name="Properties" targetFolderName="Properties">. Also you can clear the files you remove in the
<TemplateContent> from the file system (at the location where you extracted the exported template).
Zip the template and add it to the installed templates
(Windows explorer) In the directory containing the template (created in the ‘Extract the zip file’ paragraph) select all files, right click and sent to compressed folder. Name the compressed folder after the Template name that you used in step 2. The compressed folder will contain the template and is valid to use in Visual studio templates.
Normally, the project templates of Visual Studio are located in the windows user documents folder. <User folder>\<Documents>\Visual Studio 2015\Templates\ProjectTemplates. Copy the zip containing the template to that ProjecTemplates folder.
Using the template
To use the template you just created restart/open Visual Studio 2015. Choose the
Visual Studio > File > New > Project menu and in the installed templates browse to the project type/subtype that was present in your MyTemplate.vstemplate file (Step 3, or the type you used to create the template project with).
Voilá, your template should be available and you can test it / use it to create projects in the feature.
Creating a project template in Visual Studio 2015 is pretty easy, but you still have to edit the default exported template if you want it to work the right way. If you don’t remove the Properties folder from the template, all the projects that are created with the template will have the same assembly information. Also for some project types you need to add the
<CreateInPlace>true</CreateInPlace> or no files will be present in the project you create using the template.