Jan
20
2012

Creating custom project template for Visual Studio with pre-installed NuGet packages

Introduction

Recently I published an article about creating custom project template in visual studio with additional wizard window. After reading it, one of my colleague asked me what if I want to include all commonly used NuGet packages in custom project template?

So, rather than updating the old article, I thought of writing new one on the topic.

Article Body

With increasing use of NuGet packages, It is also becomes complex to remember what all NueGet packages are being used in certain projects. Also, I can find myself using many common NuGet packages in majority of applications I create (or event sample applications I create for tutorials).

Including common files in custom VS template project is good. But NuGet packages need to be installed which follows action like, updating web.config file, Adding extra folder in application, referencing new .DLL files etc.

Luckily, We can also set NuGet package in our custom project template which will automatically get installed when new project is created using this project template.

I have already covered explaination about how to create custom project template here. For the purpose of reading all information in flow, I am repeating some of the information from this old article. However, if you are interested, you can visit this article first and come back again.

Creating custom project template in Visual Studio

To start with custom template, first create a new web application project. By going to File -> New  Project -> ASP.Net web Application. Name the project as "CustomProjectTemplateNuget". I want to have couple of JavaScript files, CSS files and of course some NuGet packages to be part of this custom project template.

We will first add all the files and install required NuGet packages in this template project. Add required JavaScript file and CSS file in Scripts and Styles folder respectively

Note: This is optional step and can be avoided. Only you will need to update .vsTemplate XML file accordingly

Suppose, I want Asp.net sprite NuGet package to be automatically install by my project template and I dont want to install this package every time I create new project.

So, Now I add AspNetSprite NuGet package using console manager as shown here. To use console manager to install NuGet package, open package manager console (Go to View -> Other windows -> Package Manager Console) and install required packages by using install command.

The installation of NuGet package will create packages folder in root of your application. We will need to use this folder at later stage.

Creating .vsTemplate file

The next step is to create .vsTemplate file which will work as configuration for our template project. In root of the application, Right click and Add New Item. Select new .XML file and name it as ProjectTemplate.vsTemplate. Open file for editing and add below complete code in it.

<VSTemplate Version="2.0.0" Type="Project"
    xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
  <TemplateData>
    <Name>Blue Lemon Default Project Template</Name>
    <Description>A web site project template by Kedar</Description>
    <Icon>lemon.ico</Icon>
    <ProjectType>Web</ProjectType>
    <ProjectSubType>CSharp</ProjectSubType>
    <DefaultName>WebSite</DefaultName>
  </TemplateData>
  <TemplateContent>
    <Project File="WebApplication.webproj">
      <Folder Name="Scripts"  TargetFolderName="Scripts">
        <ProjectItem>MyJS.js</ProjectItem>
      </Folder>
      <Folder Name="Styles"  TargetFolderName="Styles">
        <ProjectItem>StandardTheme.css</ProjectItem>        
      </Folder>
      <ProjectItem>lemon.ico</ProjectItem>
      <ProjectItem OpenInEditor="true">Default.aspx</ProjectItem>
    </Project>
  </TemplateContent>
  <WizardExtension>
      <Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
      <FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName>
  </WizardExtension>  
  <WizardData>
    <packages repository="template">
        <package id="AspNetSprites-Core" version="0.4" />
        <package id="AspNetSprites-WebFormsControl" version="0.4" />
    </packages>
  </WizardData>
</VSTemplate>

The purpose of various tags inside <TemaplateData> in above XML format is evident from their name. You can change these values as per your choice (like ProjectType could be either CSharp/VisualBasic/JSharp/Web so that project template can be categorized into appropriate type).

 

Inside <TemplateData> tag, I have included <Icon> tag and specified name of .ico file. The Icon tag information is used to display picture of project template while creating new project. I strongly advice to read this to know more about template data.

The <WizardExtension> and <WizardData> are important tags here from NuGet package persepective. The wizard extension configures the wizard which will invoke the list of NuGet packages and help to load them. Since the WizardExtension uses in built assembly for initiate standard wizard, it is important to use this WizardExtension tag as it is (as below).

<WizardExtension>
      <Assembly>NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Assembly>
      <FullClassName>NuGet.VisualStudio.TemplateWizard</FullClassName>
</WizardExtension>

The WizardData tag specifies where to get the NuGet packages from (package repository). We can either use VSIX package repository or embed NuGet packages in template project itself.

The standard suggestion is to use VSIX repository, but for simple project template, embeding NuGet packages in template iteself is sufficient and works well. We will use NuGet package embeded in template itself.

To use embeded packages, we have packages repository as template

<WizardData>
    <packages repository="template">
        <package id="AspNetSprites-Core" version="0.4" />
        <package id="AspNetSprites-WebFormsControl" version="0.4" />
    </packages>
  </WizardData>

Inside that, we have to list all NuGet packages including their id and version number (This helps in getting correct update of NuGet package). Since, we have installed Asp.net sprites NuGet package, same is listed under <packages>.

Now, only following steps are required to be followed.

  • Go to directory location where you have created this project and in the CustomProjectTemplateNuget directory of the application create a file named "WebApplication.webproj".

The reason: Since we are creating this Visual Studio project template for Web site project, we have mentioned this as project file name (in .vsTemplate configuration above) This file is internally used at the time of initiating new project (For web application projects, we will specify .csproj file name).

  • Go to root directory of application and find Packages folder along with solution file. Inside packages directory, you can find separate directories for all the NuGet packages installed. Inside every package directory, there exists .Nupkg file. Copy this .Nupkg file from every package directory and copy it into root directory of application along with .vsTemplate file
  • Finally, select go to root directory. which will contain files as diplayed below. select all files and, Right Click -> send to -> Compressed (zipped) folder. This will create new zip file in the same folder

Add project template file in Visual Studio

We now need to add this zip file into default template location of visual studio. Mostly (unless you have changed) VS template stored at

\My Documents\Visual Studio 2010\Templates\ProjectTemplates\<Language>\

or

\My Documents\Visual Studio 2010\Templates\ProjectTemplates\Visual Web Developer

Read this to know how can you find default location of project template on your machine. Once the default location is identified, copy and paste the zip file here and that's it!

Using project template to create new website

To test the project template with wizard, open new Visual studio instance and go to File -> New web site. The new project name can be seen here

This will start a new web site project and install AspNetsprite NuGet package by default. This can be seen by monitoring bin folder, updated wen.config file as well as additional App_Sprites folder

Now, I have created a Custom project template which by default installes all commonly used NuGet packages whenever I create new project. Thats cool! :)

Conclusion

Visual studio can be extended very easily which help us to customize many other things. Now NuGet packages also can be added in custom templates. Although using VSIX repository is more robust and proper way to do it, For small project templates, embeding packages in template itself can suffice the need.

I hope you enjoyed reading this. Thanks for visiting my web site :)

Comments (6) -

squadwuschel

Hello,

can you please upload an example file, I've tried the Template package repository method several times but I can't get it work.

Here is my Template example:

skydrive.live.com/!1327&id=C8FE9E587BDA5762!1327

thx

matt

This didn't work for me, for C# project. File -> Add new project doesn't show the project in the list.

usha

nice article,thanks for sharing.Smile<a href="http://www.xithi.com/">; wordpress developer</a>

lopez1961

Informative writing.

perfectcolleagu57.blog.com

Hello there, I do think your site could possibly be having web browser compatibility issues. Whenever I take a look at your site in Safari, it looks fine however, when opening in I.E., it has some overlapping issues. I just wanted to provide you with a quick heads up! Besides that, excellent website!

Here is my blog;  perfectcolleagu57.blog.com - www.sharedabilities.com/.../

marketing blog

Thanks for sharing your thoughts. I really appreciate your efforts and I will be waiting for your next post thanks once again.

My web page ::  marketing blog - www.techbooth.in/.../

About Me

You are visiting personal website of Kedar (KK)

Please go here to know more about me

Disclaimer

The opinions expressed here represent my own and not those of my past or present employers.

The concept/code provided on this site may not work as described. If you are using any code provided on this site. Then, please test it thoroughly. I shall not be responsible for any issues arising in the code. 

Month List