Visual studio project templates 🚚🏗
Tags: visualstudio, beginners, architecture, tutorial
Published: 2021-09-27 21:53:42

There are companies that constantly churn out visual studio projects that mostly look the same as each other, but with different names. The problem with this is that a lot of hours are spent in setting up each project manually and it takes our attention away from the problems that we want to solve. This guide will demonstrate how to create a simple visual studio project template that can be downloaded by other developers to instantly set up their csproj layout. 

## Step 1: Picking the parts of the project that will change
After choosing the project of choice to template, we’ll need to replace the names of the namespaces and classes we feel would change with each template deployment. There a bunch of template parameters we can include [here](https://docs.microsoft.com/en-us/visualstudio/ide/template-parameters?view=vs-2019#reserved-template-parameters), but for this case we only really need one or two.

Namespaces and classes will be replaced like so: 
`namespace $safeprojectname$` || `class $safeprojectname$`

We can also name the file directly like so:
![Naming file directly in solution explorer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p8aix5udsnobszyn39s9.png)

What these replacements essentially do is take the name that is entered at the start of a project and plaster it all over the project or solution wherever the template parameter `$safeprojectname$` exists. Next up is we’re going to see how that takes place. 

## Step 2: Creating the template
When all name replacements have been made, we create the template by going to Project -> Export Template -> choose the “Project Template” radio button and Next -> Fill out the template details including the name and click the finish button.

## Step 3: Taking a look inside the template
The new template created usually lives in the visual studio folder, in an inner folder named “My Exported Templates”. This can be changed during the previous step.

![Created Template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1v0i2v8lqr3qle5hmtnu.png)

It comes in a zipped format which consists of a .VSTemplate file and a __TemplateIcon.Ico file, as well as the project files for the template.

![.vstemplate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tm918q7wsfsnk6ra5qwz.png)

Above is an example of what a .VSTemplate file typically looks like when it’s created by the template wizard. There isn’t much on this that I paid attention to except for the Name and Description, I left everything else as it was.

If we wanted to edit a class after templating, we can do it via unzipping the file, editing it and then zipping up again. Always making sure that the .VSTemplate file is in the root of the zip. The places highlighted show the `TargetFileName` attribute as well as the `ReplaceParameters` attribute (which needs to always be set to true if we want to perform param substitution).

### Using the template
When we’re done editing, we simply take the final zipped version of this template and paste to this path: Visual studio 20XX folder/ Templates/ ProjectTemplates. No need to go into the individual language folders.

![Using the template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49y74z9j1lrcsf566par.png)

Voila, it should show up in the list of project templates. Please ensure that the filters are all defaulted as it is not currently possible to search for it using them, [citation here](https://docs.microsoft.com/en-us/visualstudio/ide/how-to-create-project-templates?view=vs-2019#:~:text=(Filtering%20based%20on%20language%20or%20project%20type%20is%20not%20currently%20possible%20for%20user%20templates.)).

# Multi-project solution templates 

Having one project is a fine and dandy idea, however in more realistic situations we tend to have more than one or two projects per solution. Thankfully for us, the visual studio templates also have that ability, but with a few tweaks we need to do from our side. 

## Step 1: Export all the projects 
This is exactly what it says on the tin, for each project, repeat the steps from the previous section (except for uploading the template in the Templates folder in VS). However, instead of using “$safeprojectname$” in the files we need to prefix it with “ext_” so it will become “$ext_safeprojectname$”, this includes the file names too. Please make sure to select the correct project when arriving at the Export Template wizard pane (it should be the last option with a dropdown).

## Step 2: Extract files of zipped templates
![Files Extracted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p7vpt9nl400ixvh7lh6v.png)

As we can see the two zipped templates have been extracted into their own folders, each containing their own .VSTemplate file as expected. Those individual .VSTemplate files are where we will change one or two things. 

## Step 3: Create an external .VSTemplate
The .VSTemplate we will create outside of the extracted files will look like this:

![External .VSTemplate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h2d2xytq18v7hgwlt70r.png)

All of the lines that are highlighted in red displays some of the  differences between creating .VSTemplate file for a project vs one for a solution (such as this). These changes are required to occur for visual studio to realize that we are looking at a solution consisting of x number of projects. We can see that the type has changed from `Project` to `ProjectGroup`, the template content now contains the `ProjectCollection` and `ProjectTemplateLink` elements instead of just the simple `Project` element.

The `ProjectName` property is what we want it to be called, the element value is where it is located. The `SolutionFolder` property `Name` will give a name to our solution as a whole. 

## Step 4: Zip all in one go
![Zip all](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c2p1s72tyi8l1bn9syhr.png)

To zip all at one go, select all then right click –> Send To -> Compressed (zipped) Folder. A new zipped folder will emerge, name it as appropriate; after which, copy this zipped folder to the usual place where templates go to live happily ever after (Visual Studio (20XX)/ Templates/ ProjectTemplates) and just paste it there. We should then be able to see the template appear in the project creation wizard.

![Solution Template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17u5t4ukz1rckivn0hgl.png)

I didn’t realize that probably every single thing can be replaced, I attempted to even replace a string and it worked out! All I did was replace the “world” word with $ext_safeprojectname$, and I guess it replaced it too. [Question] I wonder if it lays the whole setup of the solution and project in a straight line of text or something, and when it comes across the first $ sign it prepares to replace until the next $ line (depending on the param name in between the two signs) 🤷‍♂️? Something to look into!

Thanks for reading 🙌 stick around for more to explore