When a team of developers must create several similar projects in Visual Studio, it is important that the projects be created as consistent and efficiently as possible. Each Visual Studio project should have the same look and feel with the same set of starter files.

Sometimes it is really handy just to have the same starting code within template files added to projects for your team. Perhaps you need a set of comments at the top that must be filled out by the developer and the files need to be consistent. Maybe the starting code files from Microsoft don’t adhere to the standards at your company. For example, when adding a class file to a C# project, Microsoft’s stock template doesn’t specify the visibility (public or internal).

Certainly custom snippets can help with this. As well, one of the easiest ways that a team of developers can enforce a coding standard is to use custom Visual Studio Project Templates and custom code Item Templates.

  • A Visual Studio Project Template is a default project and group of files with references that can be used as a new project.
  • An Item Template is a new starter file of any type.

Let’s look at creating a Project Template first. A Visual Studio project template can give a developer a stock set of .NET references and starting files. The starter project can come with stock project settings. The projects files can include a stock set of starter files (*.cs, *.docx, *.jpg, *.xml, *.anything) with started content. The code files (depending on their types) can include stock comments and types definitions inheriting from predefined base classes.

To create a project template, create a new project using the standard template closest to the one you want to create. Add all the starting files you want it to contain:

  • Add/remove project references.
  • Add additional project folders and files.
  • Change the source code of the starting files to include custom comments, using statements, base classes, and set type visibility.
  • Change any project properties that need tweaking.

projecttemplates1

When you are done configuring the project, you can click File | Export Template… to start the wizard.

projecttemplates2

Choose Project template and click Next.

projecttemplates3

Give your template a meaningful name and description. You can also assign a custom icon such a company logo so it stands out from the other templates in Visual Studio, and then click Finish.

projecttemplates4

As you can see, it will be save as a single .zip file to the custom project templates folder. As well, you can see I chose a custom icon for it.

To test it, create a new project using your new custom template!

projecttemplates5

Note that when exporting the project, you are prompted about whether to automatically register it with Visual Studio. If you have the Visual Studio SDK installed, you can even wrap the finished template in a .vsix file for deployment by using the VSIX Project template.

projecttemplates6

Your exported project templates will appear under your personal “Documents” folder. If you’re interested, you can extract the .zip file to see the contents.

projecttemplates7

The project template (.vstemplate) is actually a XML file with the root element of <VSTemplate>. All properties and project items are defined inside here.

projecttemplates8

In the root element called VSTemplate is an attribute called “Type”. This attribute can be set to “Item”, “Project”, or “ProjectGroup”.

projecttemplates9

You can also create a custom template for a Visual Studio solution containing two or more related starter projects. However, it is a manual process to create the necessary .vstemplate file referring to all other project .vstemplate files. Just be sure to set the type to “ProjectGroup”.

You can share the project template (.zip file) with other team members. It can be checked into source control with other project templates. Alternately, as already stated, you can wrap it with a .vsix installer or a NuGet package.

Besides creating Project Templates, you can also create Item Templates. For example, instead of using the stock class *.cs file included with Visual Studio, I could install and use the IntertechClass.cs file.

An Item Template is a starter code file with “using” statements and default code content (assuming C#). I could include necessary helper files as well.

To create an Item template, just select the file in the Solution Explorer window and then click File | Export Template. You will be prompted to select all the support files and necessary references for the item template.

projecttemplates10

projecttemplates11

Choose a template name, give it a description and icon, the export it. Again, you can choose whether it should be immediately imported.

projecttemplates12

After restarting VS, it should appear in the Add New Item dialog.

projecttemplates13

I hope this article helps you create visual studio project templates. It’s important to be consistent with our code for greater efficiency, simpler maintenance, and to shorten the learning curve for new team members. Happy coding!