Why a template?
Starter Kits and Templates are new features in Visual Studio (2005 and 2008) and they can be immensely useful. Every code example that you give to your customer can now be a starter kit or a project template.
Say, you have created a Web Services API to access some sophisticated functionality on your web site. Typically, you would create an example solution in C# or VB.Net and zip up the files from the solution/project and give them to your customer.
Then the user will open up that project and then learn from it (for example, how to access your web services) or copy-paste pieces of that code into his or her project, etc. This could get a bit cumbersome.
With the template feature in Visual Studio, the work you have to put into creating these templates is not that much more but the end user will be able to create a new project with that content with just a single click. Alternately, if you create item templates, the user will be able add that content into the existing projects.
Creating a Project Template
To create a new starter kit or a new project template or a new item template (i.e. a single aspx page or class file), use File – Export Template menuitem and follow the Export Template wizard.
Figure 1. A project template
In this case, I am exporting a project called FirstWebService. The original project itself was created with the ASP.NET Web Service template. That, of course, is not a problem. Let’s say you have a set of required files, references, configuration settings, etc. to create a web service using the web service API of your company.
You would include all those files and settings in the above project and prepare a template out of it. The customers using your web services will be able get started right away with using your web services, instead of going through a ton of documentation about what to include, etc.
Figure 2. Template Options
The template options of Export Template Wizard are shown above. As you can see, this template will be saved as a .zip file. I named this template ‘My Company Web Service’.
The Template .zip file
After you click the Finish button, you can see the .zip file saved in the following location: C:\Users\Administrator\Documents\Visual Studio 2008\My Exported Templates. This is the output location specified in the Export Template Wizard.
If you open that zip file, you will see all the source files from your project – no binaries will be included, of course. In addition you will see two other files:
The .ico file is the icon that shows up next to the template (My Company Web Service) in the New Project dialog box.
And MyTemplate.vstemplate includes all the relevant information to make up a project like the one you had while creating this template. This .vstemplate contains the information like the following:
- Name of template – here it’s ‘My Company Web Service’
- Description of the template – for example: ‘Web Service using my company API.’
- Project Type – here it’s CSharp
- The template icon
- Project file (.csproj) and the relevant files (.asmx, .asmx.cs, .cs, .config, etc.)
Creating a New Project out of the template
As you can see in Figure 3, the template for the C# project we saved will show up under Visual C# in the New Project dialog box. As we named it, the template shown here is ‘My Company Web Service’.
Figure 3. My Company Web Service template in New Project dialog box
The entire project you built is available to somebody for extending with just a click of a button; as mentioned before, this is ideal for developing samples, base projects, etc.