Jeremy Bostron's Blog

Project Item Templates and Project Templates

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>

Published Monday, July 12, 2004 1:23 PM by jerbos

Comments

 

Benjy said:

Hi Jeremy,
This is very interesting. Is this an enhanced version of Enterprise Templates? I've been meaning to work on Ent Templates for a long time and never actually got round to it.

- Benjy
July 12, 2004 2:00 PM
 

Luc Cluitmans said:

Funny you bring this up right now. I was playing around with it yesterday, and I like the feature a lot. I did some fiddling with templates in VS2003, but it is much easier now.

Missing an 'add new interface' item? I can now easily add it myself. Not happy with the default templates? Copy the originals to your own template directory and modify them to your liking. And don't forget that you can relocate your own template directory to a directory with a name that is easier to rememeber.

However, I filed your step 5 as a bug (bug id FDBK11710). Having to restart VS just to refresh the add item dialog is not going to encourage people to use this feature more.
July 12, 2004 11:26 PM
 

Jeremy Bostron said:

Project Item Templates and Project Templates do not replace Enterprise Templates. Enterprise Templates have additional features such as allowing the setting of rules on what a developer can or cannot do in a project. Project Templates itself are more focused on content rather then rules or policy.
July 20, 2004 3:51 PM
 

Memi.Reflection said:

August 15, 2004 7:22 AM
Anonymous comments are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker