Jason PresleyJason Presley

Visual Studio Custom Project Templates

After developing a number of web sites we tend to find a design and structure that we default to. We build out the same architecture, use the same design patterns and include many of the same resources. This can all take time to build out each project. Visual Studio provides us a way to shorten this process. It allows us to create custom solution templates that contain the structure and resources that we add to all projects.

The first step is to create a new solution the way that we always create it. We need to build out the solution using the architecture, project structure, and files that we want included in every project. I usually create a 4 tier structure with a Web, Data, Domain and Utilities layer. I include a generic repository, unit of work template, and some file and email utility classes. Below is an outline of my standard solution architecture.

Data Repositories GenericRepository.cs UnitOfWork.cs Domain
Utilities ImageUtilities.cs
FileUtilities.cs
CommunicationUtilities.cs
Web ... Standard MVC Files and Folders ...

Once the template has been created we need to export each of the projects. We do this by selecting the EXPORT TEMPLATE option. Export Template is under the FILE menu in Visual Studio 2015. In Visual Studio 2017 they moved it under the PROJECT menu. It will export the project as a zip file. This needs to be done once for each project.

After each template is extracted unzip each zip file and change the name of the .vstemplate file from MyTemplate to match the project name; Web.vstemplate for example. Place all the unzipped folders together into a common folder and add an empty text file titled root.vstemplate. To this template add the following code.

<VSTemplate Version="3.0.0" Type="ProjectGroup" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>Custom Solution Template</Name>
<Description>Custom Visual Studio Solution Template</Description>
<ProjectType>CSharp</ProjectType>
<ProjectSubType></ProjectSubType>
<SortOrder>1000</SortOrder>
<CreateNewFolder>true</CreateNewFolder>
<DefaultName>CustomSolutionTemplate</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<LocationField>Enabled</LocationField>
<EnableLocationBrowseButton>true</EnableLocationBrowseButton>
<Icon>template_icon.png</Icon>
</TemplateData>
<TemplateContent>
<ProjectCollection>
<ProjectTemplateLink ProjectName="$projectname$.Data">
Data\Data.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$projectname$.Domain">
Domain\Domain.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$projectname$.Utilities">
Utilities\Utilities.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="$projectname$.Web">
Web\Web.vstemplate
</ProjectTemplateLink>
</ProjectCollection>
</TemplateContent>
</VSTemplate>

After pasting in this code you may have to change some of the elements. You can update the name, description and the name of the template icon in the TemplateData section. You need to change the ProjectTemplateLink content match the location and name of the .vstemplate files in your exported projects. The included code matches the folder structure above that I typically use. If you want an icon included for your template you can add that icon to the root of the main folder (again making sure that you update the Icon element of TemplateData).

Finally once all of the files have been added and changes have been made zip up all of the contents of the main folder. Don't zip the main folder itself but just all of the content. Move the template folder to the Visual Studio ProjectTemplates folder which can be found at C:\Users\[YourUserName]\Documents\Visual Studio [version]\Templates\ProjectTemplates. Now you can start Visual Studio and select your template under the C# project type of search for it by name.

<< View All Posts