The last few days I have been playing around with a new Visual Studio 2005 feature called Project Item Templates and Project Templates found in Beta 1.  Essentially it is a very cool way to create code and then have it show up inside of Visual Studio dialogs.  For example, a “Project Item Template” shows up in the “Add New Item” dialog when you select the “Project” menu and choose “Add New Item”.  A “Project Template” shows up in the “New Project” dialog when you select the “File” menu and choose “New Project”.

What is very cool is these templates are easily created and plugged into Visual Studio.  Below is a basic way to get started with an Project Item Template.

  1. Create a class file (test.cs).
  2. Create a file with a .vstemplate extension (testTemplate.vstemplate).  This is basically the file that contains XML that the designer can read to understand how it should display your template.  See code sample below..
  3. Add the test.cs and the testTemplate.vstemplate to a compressed folder (testTemplate.zip).
  4. Add the testTemplate.zip to the appropriate ItemTemplate folder found in your “My Documents“ folder.  For example, since I did this in Csharp I placed the file in: 
    My Documents\Visual Studio\ItemTemplates\Visual C#
  5. If you have the Visual Studio open when you pasted in the .zip file you will need to exit Visual Studio.  Restart Visual Studio, open your project and select the “Project” menu and choose “Add New Item”.  Scroll the Add New Item dialog to the bottom and you will see a section called “My Templates“ and the custom item template you created should be found there.

Additionally, if you want the template to show up in the “Add New Item“ dialog with all the standard templates and not in the My Templates section, you can add the .zip file to the following location:
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033

When performing this last step and placing the template with all the standard items it is important after you past the .zip file to the path above, you will need to run “devenv /setup“ from a command prompt, with Visual Studio not open.  This is a bug in beta 1 that is being worked on. 

I would love to hear any feedback that you have about this feature.  Below is the sample code for the .vstemplate file that I discussed.  For more information you can check out the online docs.
-Jeremy

<VSTemplate Type="Item" Version="1.1.1">
  <TemplateData>
    <Name>
      <String>Testing an Item Template</String>
    </Name>
    <Description>
        <String>A test of an item template</String>
    </Description>
    <Icon>
        <FilePath>Litening.ico</FilePath>
    </Icon>
    <ProjectType>
      <Languages>
          <Language>CSharp</Language>
      </Languages>
    </ProjectType>
    <DefaultName>Test.cs</DefaultName>
  </TemplateData>  
  <TemplateContent>
    <ProjectItem>
      <SourceFile>Test.cs</SourceFile>
    </ProjectItem>
  </TemplateContent>
</
VSTemplate>