Creating a Visual Studio Project Template

I wanted to start using the Model View ViewModel design pattern more often so I thought I would download a Visual Studio Template I could use when I create new projects. Well it seems like everyone is still a bit behind when it comes to creating templates for Visual Studio 2010. I did find a nice sample solution that contains all the basics. You can find it here This was good but I really didn’t want to have to go through editing each file every time I want to create a new project so I decided to try and export this as a Project Template


Exporting the project

  1. The first issue I had was that the “File > Export Template” option did not support exporting multiple projects. I did find a nice extension that allows you to do this though. You can download it from here or in Visual Studio 2010 go to Tools > Extension Manager and search online for “Export Template Wizard”

Once it is installed you will have an option in the file menu called “Export Template as VISX” but we need to make some modifications to the solution first so the template will use the project name we type when creating a new project. Visual Studio Templates use Template Parameters to replace parts of your projects with predefined variables. The only one I used was $safeprojectname$ but there are some other parameters that you might find useful such as $safeitemname$ if you’re creating an Item Template rather than a project template.

  1. This will break the solution and you won’t be able to build it anymore so create a copy first.
  2. Change the Project Name and Solution Name to $safeprojectname$. I Left the second project as CoreMVVM but I think you can rename it to something like $safeprojectname$.CoreMVVM but I have not tried this

  3. Normally the namespace would be the same name as what you had entered into the Add New Project dialog. To do this you need to replace any reference to the namespace with the $safeprojectname$ parameter. In this example I just replaced any reference to “WpfModelViewApplication2” to “$safeprojectname$ using the replace feature in visual studio. Make sure to replace the namespace in the xaml files and the in the AssemblyInfo.cs so it looks like this

  4. Now save the changes and go to File > “Export File as VISX”. Select Project Template, check both projects and select next

  5. Enter a Template Name, Description, Icon and Preview Image and select Next

  6. Change the Product Name to the name of the Project Template and fill any out any other details. Then select Finish

Using the project template

  1. If you didn’t select the option “Automatically import the template into Visual Studio” on the last step double click on the .vsix file that was created to install it. This will then show in the Extensions Manager in visual studio
  2. In Visual Studio select “New Project” and find the new template

  3. Build the solution and the end result should look something like this. Note that the namespace has replaces any space characters with the underscore ( _ ) character

  1. #1 by HighHopes on September 5, 2011 - 12:40 pm

    Hi, i followed the steps for creating a simple class library project. But the namespace haven’t changed. When replacing the namespace with $safeprojectname$ it gives an error, that “$” are invalid characters. It replaces these characters with underscore “_” . So after creating a project from the template , namespace is still _safeprojectname_ . Is there anything else to do ?

  2. #3 by HighHopes on September 7, 2011 - 11:31 am

    I solved the problem i mentioned before. There was a step as “I just replaced any reference to “WpfModelViewApplication2″ to “$safeprojectname$”. I thought after replacing the values the solution needs to be builded again. but it doesn’t need to be builded successfully. After renaming a creating the template, problem solved.

    But when i tried this in a multi project solution, i faced some problems.
    – First one is, If the projects in the solution gives references to each other, after creating solution from template , you need to remove first then add the references again. Because n solution explorer, in the “References” part, the names of the references remains $safeprojectname$. The names of the projects are changing but the references aren’t updated.
    – Second one is i need to give the file names of a project dynamically too. Forex; I have a web project XXX.Web and under this project i have a webpage as XXXWebPage. The “XXX” part must be the project name. After renaming the project name as $safeprojectname$.Web , the project name is changing while creating the new project from the template. but the file name remains $safeprojectname$.WebPage. Is it a way to rename the files’ names under the project , with the project name as well ?

  3. #4 by shiva on October 16, 2012 - 4:39 am

    references are not adding dynamically when we creating new project from template. we need to add the reference manually . Is there any other way to make the references to add dynamically while exporting the template ?

    • #5 by RBurnham on October 16, 2012 - 8:09 am

      Looks like you need to add the binaries to the solution/project before you create the template. For example if you use nuget make sure the packages folder is included in the solution (show all files > right click packages > include in project). a solution level dependencies folder should also work but i haven’t tried. If you do this the wizard will include them in the template. Apart from this you could try exit the zip template it creates.

  4. #6 by Otto's Striphandel on October 24, 2014 - 11:49 am

    After I made an export I want to use the template. When I chose the template the project is made but I always have problems with installed references and nugetpackages that are included. The give errors and what ever I try I can’t get rid of it. Can some one help me out with it? Thanks a lot

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: