Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#
Article

Visual Studio .NET "Add New Item" Custom Template Installer

Rate me:
Please Sign up or sign in to vote.
4.15/5 (12 votes)
20 May 20044 min read 91.6K   280   42   9
Tool to automate the installation of Custom Item Templates in Visual Studio .NET.

Introduction

Adding a project template entry into Visual Studio .NET's "Add New Item" window seems like it should be a simple process, until you try it. The multi step, multi file process is cumbersome and error prone. Chris Sells' article clearly defines the process, but if you have to add more than one template, you soon realize there has to be a better way.

Background

Like all developer tools, this one came to being to make my life easier. I had several project item templates I wanted to reuse and wasn't about to manually process each one. The nuances of creating vsdir, vsz, js and inf files, inserting items in pipe delimited strings (yes, I said pipe delimited!), and putting the 5 or 6 files in a proper directory structure is too much to expect anyone to get right each time. After studying Chris' article, I boiled down the process into a few manageable items and started coding.

Since I wanted to be able to install multiple templates at a time in a simple fashion, I chose a console application that uses XML files as input. This way, a simple batch file can install as many templates as needed with one command. The XML input requires the basic information needed to create the project item, and isolates the user from knowing how or where to create the required files.

The Simplified Process

The developer now has only two tasks to complete before installing the custom template:

  1. Create the template file(s) themselves. (There are many articles on the details of creating these item templates. This article shows a good example for a custom web form: MSDN.)
  2. Define a few details in the XML file. See the XML definition below.

The gory details are handled by the installer application.

The XML File

XML
<?xml version="1.0" encoding="utf-8" ?>
<NewProjectItemTemplate xmlns="<a href="http://newprojectitem/NewProjectItemDefinition.xsd">http://NewProjectItem/NewProjectItemDefinition.xsd</a>">
   <TemplateName 
      Name="" ImageType="" ProjectType="" 
      ProjectSubType="" NewSubTypeName="" />
   <ShortDescription></ShortDescription>
   <LongDescription></LongDescription>
   <DefaultFileName></DefaultFileName>
   <CodeFileTemplatePath></CodeFileTemplatePath>
   <WebFormTemplatePath></WebFormTemplatePath>
</NewProjectItemTemplate>

The application uses a typed DataSet to read the XML input file and uses the resulting object properties to create the project item. As it stands today, you need one XML file per project item you want to install.

Element and attribute definitions:

  • Name: what you want to call your new project item (e.g., CsharpAddNewWidget)
  • ImageType: which icon to use to display the project item in the "Add New Item" window. Allowable values: WebControl, WebForm, XmlFile, Class, Component, WebService, WebUserControl, WindowsService, WindowsForm, WindowsUserControl, WindowsCustomControl. (Note: all enumerated values are fully defined in the XSD file.)
  • ProjectType: Determines which project type folder the project item will display under, Local Project and/or Web Project. Allowable values: LocalProjectItems, WebProjectItems, Both.
  • ProjectSubType: Determines which project item subfolder the item will display under. Allowable values: Code, UI, Data, Resources, Utility, New.
  • NewSubTypeName: If you chose "New" for ProjectSubType, provide the name of the new folder to create and display in the "Add New Item" window. Note: if the new type already exists, the project item will be placed in it, leaving the existing entries alone.
  • ShortDescription: The name to display with the icon in the "Templates" list view.
  • LongDescription: The name to display in the info area when the template icon is selected.
  • DefaultFileName: The default file name to put in the "Name" text box of the "Add New Item" window.
  • CodeFileTemplatePath: The path of where to find the code template to use. (If a full path is not provided, the app looks in its directory for the file.)
  • WebFormTemplatePath: If you are creating a custom web form, this is the path of where to find that file. (If a full path is not provided, the app looks in its directory for the file.)

Command Line

The command line processing is straight forward and the help file can be viewed with a "/?" flag. The installer takes up to 3 parameters.

  1. The XML input template file (full path if not co-located with the executable)
  2. The path to Visual Studio .NET 2003 (optional if installed in the default location of C:\Program Files\Microsoft Visual Studio .NET 2003.)
  3. "/s" - silent mode. When running in a batch file, it doesn't wait for user input to run the next statement.

Output

For each run of the installer, you will get a simple progress output letting you know what the installer is doing. If you are re-installing a custom template, you will also get two "removing" lines as the installer deletes the old templates and add the new ones. Once complete, you can open the "Add New Items" window, see your new entries, and add them to your projects.

Image 1

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSafely removing new items? Pin
MrGlover14-Jun-06 15:27
MrGlover14-Jun-06 15:27 
GeneralVery Helpful Pin
timbochu22-Feb-06 20:15
timbochu22-Feb-06 20:15 
QuestionCould it work for WebUserControls? Pin
valdonano26-Aug-05 4:08
valdonano26-Aug-05 4:08 
AnswerRe: Could it work for WebUserControls? Pin
Michael McKechney5-Sep-05 15:04
Michael McKechney5-Sep-05 15:04 
GeneralRe: Could it work for WebUserControls? Pin
Rogerio Meneghelo Apezzatto12-Mar-08 6:17
Rogerio Meneghelo Apezzatto12-Mar-08 6:17 
Generalonly for C#... Pin
TCDooM27-Jul-05 23:22
TCDooM27-Jul-05 23:22 
GeneralShort and concise Pin
vannich16-Jun-05 15:16
vannich16-Jun-05 15:16 
GeneralRe: Short and concise Pin
Michael McKechney16-Jun-05 18:14
Michael McKechney16-Jun-05 18:14 
GeneralVery nice article... Pin
Maruis Marais21-May-04 14:56
Maruis Marais21-May-04 14:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.