Speed Up with Project Templates Using Visual Studio 2005 - Part 2
 
Published: 26 Jul 2006
Unedited - Community Contributed
Abstract
In this second part of the article, Ameet demonstrates how to create Project and Item Templates using Visual Studio 2005.
by Ameet Phadnis
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 53378/ 929

Introduction

Since my very first project I have been trying to find easy way out of coding or a way to jump start my projects.  My very first article published on ASPAlliance also dealt with how to speed up projects using project templates in VS 2003.  Readers really liked the process explained in those articles and I received some good reviews on it.  After the release of VS 2005, I have been getting e-mails about how the previous process can be utilized for VS 2005.  Frankly, the process in VS 2003 was too complicated and with VS 2005 the readers will be surprised how easy it is to build templates and share it with the whole team.  If anyone is interested in how it was done in VS 2003 please refer to this article.

Background

So, why do we need templates anyway?  In the corporate scenario the development team has to deal with many projects at the same time.  Almost all the projects do have branding or a similar look and feel to identify the corporate image.  In a normal scenario the developers would be designing the same pages, creating or copying CSS files from other projects and incorporating them within the new project.  Instead of doing all the copying and pasting of code from different projects, it would make sense to have a startup project template from which you can start building the business logic for the application in the project template.

In our consulting company scenario, we do build projects for service providers.  The service providers would like to have ASP.NET 2.0 applications which will allow users to login, register their company, register individuals, etc.  In this article I would like to demonstrate how you can create a Web Template.

Structure

Before we start, I would like to explain the current template structure in VS 2005.  VS 2005, by default, comes with few templates to jump start projects.  For example, for Web Applications the developer can start a project using ASP.NET Web Site, ASP.NET Web Service, Personal Web Site Starter Kit, Empty Web Site or ASP.NET Crystal Reports Web Site.  When the developer clicks on File -> New -> Web Site, they are presented with the following screen to choose the kind of Web site they want to create

Figure 1

The IDE picks the template information from the following directory:

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\Web\VisualBasic\1033

The path mentioned above is based on the assumption that the developer had selected the default options while installing VS 2005.

If you take a look at the files under the above folder, you will notice that for each of the Web Options you have a zip file.  This zip file contains all the files needed to create the project.  It also contains one extra file with the extension as vstemplate.  This file contains all the information it needs to create the project and add appropriate files to the project.  The vstemplate file stores the Meta data on the project templates in XML format.

In short, to create a template you need to create a vstemplate file and a zip file of all files needed to create the project.  However, the question arises about where we store the zip file.  The readers would assume the zip file is stored under the same folder, but in reality VS 2005 allows you to store templates wherever you want.  You can even store the templates on the network drive.  This is a big help for developers working in a team.  In order to make VS 2005 aware of these templates we need to make some minor changes.  You can follow these steps to let VS 2005 know where the user defined templates are located.

1.      In VS 2005, go to Tools->Options.

2.      Expand Projects & Solutions.

3.      Click On General.

4.      Make the necessary changes in Visual Studio user Project Templates location and Visual Studio user Item Templates location.

5.      By default, the options should be pointing to C:\Documents and Settings\<User>\My Documents\Visual Studio 2005\Templates.  This can be changed to point to a network drive too.

Once the above steps are followed on each of the developer's machine, the developers can create a central repository of all the project templates to be used in the organization.  So, each time either a project or item template is created, those templates can be pushed to this location.  In the next few sections I will explain how to create Project Templates and Item Templates.

vsTemplate File

The vstemplate is made up of the following.

·         TemplateData - Stores information on how to display the template information in New Project or Add New Item Dialog box.  Important elements of TemplateData are the following.

·         Name: The name that gets displayed in the New Project / Item dialog box.

·         Description: The description of the template that gets displayed on the New Project / Item dialog box after the Name has been selected.

·         Icon: The images file to be displayed with the Template in the New Project / Item dialog box.

·         ProjectType: Used to categorize the Project Template so it appears in the specified group in the New Project or Add New Item.  The ProjectType can have the following values.

·         CSharp: For Visual C# project / Items

·         VisualBasic: For Visual Basic Project / Items

·         JSharp: For Visual J# Project / Items

·         Web: The template used to create Web Projects.  If this ProjectType is selected, then to specify the language you need to specify the ProjectSubType element.

·         TemplateContent - This specifies all the template contents.  Elements of TemplateContent are the following.

·         ProjectCollection: Used to define the organization and contents of multi-project templates.

·         Project: Specifies all the Items, like files and folders, which are included in the project. Elements are either Folder or ProjectItem.

·         References: Specifies all the assembly references required for the project.

·         ProjectItem: The file that has to be included with the Project Template.

·         CustomParameters: Used with Wizard Templates.  The custom parameters are passed to the template wizard to make parameter replacements.

·         WizardExtension - Contains the registration information for customizing the template Wizard. It will contain the assembly information and the full class name.

·         WizardData - This is custom XML string that is passed to the custom wizard extension.

This article will focus on the first two elements.  WizardExtension and WizardData are out of scope of this article.  I will try to publish another article which will deal with WizardExtension and WizardData in the near future.

Creating an Project Template

In my previous article I have explained how to create templates in VS 2003.  Many users might have noticed it required too much work and the developer also needed to know how all different files come together and how to use JavaScript to create the project.  Now the developers can relax since in VS 2005 it takes only 5 steps to create a template.

Steps to create Project Templates in VS 2005

·         Start with a pre-defined template.

·         Make your changes, like adding forms, classes, etc.

·         Save the Project.

·         Click File -> Export Template Option.

·         The Wizard will walk you through creating the Project Template.

Please note: Some users might not see the Export Template option in their File Menu.  You might need to add that option by following these steps.

·         Right Click on the toolbar and select Customize.

·         Click on the Commands tab.

·         Under Categories, select File.

·         Under Commands Select Export Template and drag the command under File Menu.

To demo the project template creation, I have created a website called templatearticle.  The folder structure looks like the following.

Figure 2

To create the Project Template from the above structure, follow these steps.

·         Click File -> Export Templates.

·         The Export Template Wizard displays the below figure.

Figure 3

·         Choose Template Type Step has the following options.

·         The Project dropdown will display all the projects currently in the solution.  In this example we just have one project.  So, only that project is displayed in the dropdown.

·         For Language it will display all the different language options available.  In our case it should display Visual C#, Visual Basic and Visual J#.  I will select Visual Basic for this example, but in normal cases you would select both Visual Basic and Visual C#.

·         If you wanted to export an item, you would select the Item Template option.  This option is explained in the next section.

·         Click Next.

·         The Select Template Options step displays the following.

Figure 4

·         The options for this step are the following.

·         Template Icon - You can select the image you want to display with your template in the new Project dialog box.

·         Template Name - This is the name that will be displayed in the New Project dialog box.  For this example I have entered Template Article

·         Template Description - This will be displayed as description on the New Project dialog box.

·         Automatically import the template into Visual Studio - This option will store the template in the directory you have selected in your options dialog box (explained earlier).  If you uncheck this option then it will store the template on the directory, shown above in the image. Once the template is created you can simply copy the template to the folder from where your templates are being picked for display.

·         Display an explorer window on the output files folder - This option will display the folder in explorer window where the zip file for the template is stored.

·         Click Finish.

·         Based on the options you have selected, the template that will be created.  It will also display the folder in explorer window if you have selected the option of explorer window on the output files folder.

·         If you check your templates folder, the above options will create a zip file.  If you look inside the zip file, now you will see a vstemplate file.  You can open the vstemplate file and see all the options exported while creating this template.  Also, please note that the zip file contains one extra file called _TemplateIcon.ico.  This Icon will be used in the New Project / Item dialog box.

Creating an Item Template

The above section explained how you can create a Project Template.  Creating an Item Template is similar to creating a project template.  To explain the creation of Item Template, I will use the same example above and export the CompanyRegistrationInfo class.

Steps

·         Click File -> Export Templates.

·         The Export Template Wizard displays the following figure.

Figure 5

·         Select Item Template.

·         Click Next.

·         The Select Item to Export step displays the following.

Figure 6

·         Check the item template you would like to export.

·         In my scenario I have selected CompanyRegistrationInfo.vb.

·         Click Next.

·         The Select Item References step is then displayed.  You can select which references need to be created when you create an item from this template.

·         For my example I have not selected any.  Click Next.

·         The Select Template option step displays the following figure.

Figure 7

 

·         Give the appropriate name and enter a description.

·         Click Finish.

·         The item template gets created in C:\Documents and Settings\<user>\My Documents\Visual Studio 2005\Templates\ItemTemplates.

Using the Project and Item Templates

With the above steps we have created Project and Item Templates.  So, let us see how the template looks when you try to create a website or add an item.

Creating Project from Project Template

·         Click on File -> New -> Web Site.

·         The new Web Site dialog box displays the following.

Figure 8

·         Under my templates, notice your new template has been added called Template Article.  Please note that the Template Article is the name you gave the Template on the final step of the export.  Also, the Description is displayed just above the Location information.

·         When you enter a site name and click "ok," the project is created using your template.

Creating a Item from Template

·         Right Click on Project folder and select Add New Item.

·         The Add New Item Window is displayed.

Figure 9

·         Note the Registration class which we had created earlier is displayed under my Templates.

·         Once the class is created, it will be created under the name you specify in the above image.  In our example it will be RegistrationClass1.vb.

·         In order to find out how it assigns the name, you can open the VB file which is stored under the zip file for the Item Template.  You will realize that while you created the Item template, it added $safeitemname$ to the class name in the VB file.  Also, if you look at the vstemplate file, you will note that it added $fileinputname$.vb to the TargetFileName attribute to ProjectItem.  That is how the file is created for the class.

Summary

In this article I have explained how to create a project template and Item template.  I have also tried to explain how the template's folders are structured on your machine and how you can create a central repository to store all your company templates.  It also gets into the depth of the vstemplate file structure.  Using the above steps and creating templates will save the developers a lot of time and give them a jump start on Templates and Projects.



User Comments

Title: Urgent requirement   
Name: Arijit chakraborty
Date: 2009-02-18 12:36:19 AM
Comment:
dear Ameet,

I am a final year B.Tech student on a project of Online Examination System(ASP.Net).We r facing very problem when we r trying 2 import downloaded templates in our project.

If u kindly help us 2 do so,it will b great for us.Hope 2 hear u soon.

Thanks-
Arijit Chakraborty
mail_arijitchakraborty@rediffmail.com
Title: Thank you, Thank you, Thank you!!!   
Name: Dave
Date: 2008-06-13 3:38:31 PM
Comment:
Thank you for taking the time to put this information together. This info is exactly what I needed and I didn't have to figure it out from piecing together tidbits of information spread across multiple poorly linked MS documentation and kb articles. Your efforts on this article are greatly appreciated!
Title: Location field missing in templates   
Name: Cheryl
Date: 2008-04-06 10:59:03 PM
Comment:
Help!!! Like Jai, the location field is hidden on all my pre-existing templates. How does this happen and how do I fix it?

thx,

Cheryl
Title: Thanks, it helps   
Name: elena
Date: 2007-08-01 8:20:28 PM
Comment:
Thanks for the tutorial, it expalined very well, though I am looking to create template for solution with few projects. Initially I read microsoft article and it was crap. Yours - great.
Title: Great Tutorial!   
Name: Ashley
Date: 2007-07-16 5:34:52 PM
Comment:
You're tutorial was very helpful! The "Personal Web Site Starter Kit" option was a nice find! I've always developed my sites from scratch! Very nicely written!

Thank you!
Ashley
Title: My Export Wizard is grayed out   
Name: Dannyhut
Date: 2007-06-26 6:52:13 PM
Comment:
Hi,

I have created a template but when I select the File Menu, my Export Wizard Menu Item is there but is grayed out and I can not select it. Please can any one help.

Thanks
Dannyhut
Title: Customizing the project template   
Name: AP
Date: 2007-06-20 5:33:07 PM
Comment:
Check this article and it should help you out http://aspalliance.com/1001_Speed_Up_with_Project_Templates_Using_Visual_Studio_2005__Part_3
Title: Customizing the project template   
Name: Moupiya
Date: 2007-06-20 1:22:00 AM
Comment:
HI Ameet
Nice article.It ehlped me to know many things.I have a query.I created a project template.Now i want when the user selects my project template then he is asked for whats all things he wants to select from the project template.
Like while i created the template project i added different folders.But the user may/may not need all the things.So when he selects my project template he is asked for the options
Thanks in advance
Moupiya
Title: Thank you   
Name: DKB
Date: 2007-01-26 8:12:13 PM
Comment:
Thankyou for all your efforts in giving us this project to use. Is there somwhere in this site that helps with design of a web template know that would compliment these instructions just great
Title: Doubt   
Name: Thiyagarajan.N
Date: 2006-12-11 11:45:30 PM
Comment:
Hi, I have a doubt while create the Project template.I want to create a Project template for 3-Tier web application in a single template.It should contains one Presentation layer,BLL and DAL of solutions. How can i do that?

thanks
Thiyagarajan.N
Title: Missing Templates Problem   
Name: AS
Date: 2006-11-15 8:51:30 AM
Comment:
Well, this seems to be a known "hairy" problem. I have not found a solution to my problem just yet, but at least I know that it is a problem and some people have tried to solve it. I hope this link helps if you are experiencing it:

http://geekswithblogs.net/ehammersley/archive/2005/11/08/59451.aspx
Title: Still have a question   
Name: AS
Date: 2006-11-15 8:38:48 AM
Comment:
Good article, thanks!

I have tried the steps that you mention.
However, when I go to create a new project I do not see the template that I just created. I can see the zip file that was created, but I do not see the specific template that I just created. I also tried re-setting, as suggested by Mike before, but it does not work. Any suggestions? I'd like to use templates to work with C# Windows applications - not web apps. Thank you again for your time!
Title: Thank you buddy   
Name: Murat YILMAZ
Date: 2006-11-07 2:41:04 AM
Comment:
Hey,
Thank you buddy for your article.
Title: Edit item template   
Name: Firoz
Date: 2006-10-06 4:36:28 AM
Comment:
A very good explanation of templates. Thanks.

One question though, can one edit an existing item (or project) template?
Title: very helpful   
Name: Amanda
Date: 2006-10-05 3:54:56 PM
Comment:
Your article answered all of my questions and then some that I hadn't even thought of asking - thanks!
Title: Thank You!   
Name: Jeffrey
Date: 2006-09-29 11:52:12 AM
Comment:
Thank you so much for this article and the way it was written. As a fairly new user, I was totally lost on the use of templates until I read your article. Few if any of the books that I have purchased ever even discussed templates. Thank uou once again.
Title: My Templates? (Got it!)   
Name: S10
Date: 2006-09-27 11:04:37 AM
Comment:
The templates are under the Visual Basic root node in the New Project selection UI. I was looking under Windows and Starter kits and Other Project Types!
Title: My Templates?   
Name: S10
Date: 2006-09-27 10:49:48 AM
Comment:
I've created a template for a DLL project instead of a Web Site. The Zip file has been created and is placed in the same directory as referred to by the Tools -> Options -> General -> User templates location directory.

However when I create a new project and use the newly created template, I do not seem to be given the "My Templates" bit of the UI! I've tried going into "Other Project Types"->Visual studio solutions as well, but the "My Templates" doesn't appear.

I've used VB.net to create the template. Does this matter?
Title: Export Template option   
Name: Ameet
Date: 2006-09-16 2:43:09 PM
Comment:
Follow these steps. Its taken from the article itself

Please note: Some users might not see the Export Template option in their File Menu. You might need to add that option by following these steps.

· Right Click on the toolbar and select Customize.

· Click on the Commands tab.

· Under Categories, select File.

· Under Commands Select Export Template and drag the command under File Menu.
Title: Reset Import and Export Settings   
Name: Mike
Date: 2006-09-16 9:23:30 AM
Comment:
If your Export Template Option is not visible, I just made if visible by Resetting all settings in the Import and Export Settings Wizard.
Tools->Import and Export Settings ->Reset all Settings
I can see the option now!
Title: Click File -> Export Templates   
Name: Mike
Date: 2006-09-16 8:37:24 AM
Comment:
Any hint on how to make the Export Template option visible?
I'm using VS 2005 Professional.
Title: Gud   
Name: MZH
Date: 2006-08-29 1:44:52 AM
Comment:
Very Very gud
Title: Excellent   
Name: Harry K
Date: 2006-08-19 11:20:39 AM
Comment:
Now I know how to create a project and item template of my own. Many thanks for the concise step by step illustration.
Title: Location Field   
Name: Jai
Date: 2006-08-18 12:48:05 PM
Comment:
when i created a project template and tried to use it the location field was hidden and i couldnt create my site at a different location, could you help me as to why it happens.

its a great time saver to work new projects this way.

thanks.
jai
Title: Well put together...   
Name: Yogesh R
Date: 2006-08-18 10:47:22 AM
Comment:
your article is well put together. Since create best practices and templates are prone to change as we identify new standards and requirements, is there a way to change an existing custom template or Do we just need to create new projects from existing templates and create new templates from them?
Title: Thanks   
Name: AD
Date: 2006-08-07 8:34:41 AM
Comment:
Very useful -- many thanks for the information.
Title: Nice!   
Name: Tony A
Date: 2006-08-05 11:42:51 AM
Comment:
Nice article Ameet: Well and concisely written together with good illustrations: A great help:

Thanks and keep up the good work:

Regards

Tony A

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-05-22 7:18:06 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search