Monday, January 08, 2007

Introduction

Why do you need them? We could give you a short answer about this.  When developing several projects in a company, you want them to have the same look.  So, just like me, for every new project you copy files, code, etc from a previous project.  The danger is that in the first place you forgot some items and then you have to add them later.

This all takes up a lot of work.  You could help yourself and your team by creating a project template.  When this template is installed you can use it, just like when you start to create a new project with one of the standard templates.

VS2005 templates

First, let’s take a look where all the templates can be found.  The standard templates can be found at this location: C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Web\CSharp\1033 for the CSharp web templates.

If you create your own templates they will be placed under: C:\Documents and Settings\<User>\My Documents\Visual Studio 2005\Templates.

You can change the location of this.  To do so follow the next steps:

  1. Start VS2005
  2. Go to Tools/Options
  3. Expand Projects & Solutions
  4. Click on General
  5. Make your changes for Visual Studio user Project Templates location and Visual Studio user Item Templates location

Difference between project and item templates

Item template

An item template is an individual item that a user can add to a project by using the Add New Item dialog box. 

Project template

Project templates contain complete projects and can be added by the user with the New Project dialog box.

Inside a template

With this you could put the templates somewhere on a network drive and everyone connects to this drive.  So you won’t have to deploy the templates to every developer.

When taking a closer look to the standard templates, you see that a template exists of a zip file containing all the files needed to create your project/item.  The most important file is the .vstemplate.  This file contains the definition how your project should look.  This file contains several blocks

  • TemplateData
  • TemplateContent
  • WizardExtension
  • WizardData

At this moment I will only focus on the TemplateData and TemplateContent block.  So let’s take a closer look at them.

TemplateDate

This is the definition how the template is displayed in either New Project or Add New Item.
Several attributes can be set.  We list the most used.

Name: Specifies the name of the template
Description: The description of the template
Icon: The icon that will be displayed
ProjectType: the category under which it will be appearing.  A Category could be CSharp, VisualBasic,…

A full list can be found here.

TemplateContent

This block specifies the contents of the template.  This block doesn’t have any attributes, but has a few child elements.  With the child elements you could define your projects, references, project collections and more.

Read the info here.

Create your template

When you have created templates in VS2003, you know it took a long time to go. If you don’t know how this is done, read more info here.

From VS2005 it only takes a few steps to create your template.  We will explain the first steps and then split up in the two types of templates.

First steps

  • Create a new project of the type you want to create your template.
  • Change the project for your needs by adding your forms, classes, etc.
     
  • Save the project
  • Go to file/Export template
  • A wizard pops up
  • Now choose your template style and if you have more than one project defined, the project you want to export.

Project template

After choosing the project template you get the next step which leads you to this screen.

Fill in all project items as described above.  If you check the automatically import the template into Visual Studio, you don’t have to copy manually your template in the user template folder.

Press the finish button and your template will be generated.

If you check now the template file, you will see the zip file, containing your exported project.

Item template

If you choose for the item template, you will get the following screen as next step.

Now check the item you want to export.  After pressing next you will get a new wizard screen.

On this screen you can add the references, if needed by your selected item.  Press again next and you will get the item properties wizard screen.

Press finish to create your template.

I don’t see the export template menu item

It seems that sometimes the menu item isn’t visible.  If you follow the next steps the item will be available.

  • Right click on the toolbar
  • Select Customize
  • Click on the command tab
  • Under Categories and select File
  • Under commands select Export Template
  • Drag this under File Menu

Use your templates

Now that we have our templates, we can use them.  Open Visual Studio and choose create new project.  You will get a screen like this

On the right side you will see a section My Templates where you find your project template.  Choose the template, give it a name and press OK. The project will be created.

We also have created an item template.  Add a new item to your project.  You will see next screen. 

-

Also here you will find a section My Templates.  Choose your template, give it a name and press OK.  The item is created in your project.

Customize your templates

All templates support parameters so you could replace class names, namespaces,… on the moment the template is initiated.  These parameters are replaced by the wizard, the moment the user clicks OK on the New Project or Add New Item.

A list of all reserved parameters can be found here.

Multi-project templates

Multi project templates are in fact containers for 2 or more projects.  When choosing the template in the New Project dialog box, a solution with all included projects is created.

In common a multi-project template is almost built in the same way as a project template.  It exists out of

  • All projects
  • Each project has its own .vstemplate file
  • 1 root .vstemplate file

The root .vstemplate file differs from a .vstemplate file of a project.  It contains a ProjectCollection element.  This element exists out of 1 or more ProjectTemplateLink’s.  Nice to know is that a multi-project template can contain projects written in different languages.  It will be placed in one category of the New Project by using the ProjectType element.

The creation of the project part is the same as a project template.  What I do is first creating for all the projects the templates.  Unzip them.  Then create your root .vstemplate file.  This file contains your metadata for the multi-project template.  Afterwards right click on the files, select send to and click Compressed (zipped) folder.  Place this zip file in your Visual Studio template folder.

There’s one disadvantage when using multi-project templates. Individual projects cannot be assigned by the New Project dialog box.  Instead, the wizard will use the ProjectName attribute on the ProjectTemplateLink to specify the name of each project.

You can solve this by using the Guidance And Automation Toolkit (GAT).  At this moment this tool isn’t officially released yet.  But you can download the CTP June 2006 here.

I will blog later about this tool.  So stay tuned.

1/8/2007 1:58:44 PM (Romance Standard Time, UTC+01:00)  #     |