Visual Studio .NET "Add New Item" Custom Template Installer
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.
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:
- 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.)
- 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 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.,
ImageType: which icon to use to display the project item in the “Add New Item” window. Allowable values:
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:
ProjectSubType: Determines which project item subfolder the item will display under. Allowable values:
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.)
The command line processing is straight forward and the help file can be viewed with a “/?” flag. The installer takes up to 3 parameters.
- The XML input template file (full path if not co-located with the executable)
- The path to Visual Studio .NET 2003 (optional if installed in the default location of C:\Program Files\Microsoft Visual Studio .NET 2003.)
- “/s” – silent mode. When running in a batch file, it doesn’t wait for user input to run the next statement.
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.