AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=933&pId=-1
Speed Up with Project Templates Using Visual Studio 2005 - Part 2
page
by Ameet Phadnis
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 43933/ 92

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.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 11:33:54 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search