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.
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
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=22.214.171.124, 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=126.96.36.199, 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>\
\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! 🙂
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 🙂