Share via


Defining Item Templates

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies.
This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

An Item template unfolds itself into a project item, such as a class. If a Guidance Package is enabled, Item templates that are referenced in the Guidance Package can appear in the Add New Item dialog box, if they are located in the Items template root folder, or one folder below, in the Guidance Package solution structure. An Item template never enables its Guidance Package, and it can be unfolded only if the package has been enabled manually, or if the Guidance Package was started when a solution was created from a Solution template.

Note

Note: For an Item template to appear in the Add New Item dialog box, there must be a reference associating the template with the solution element that is selected. For more information, see Understanding Visual Studio Templates.

An Item template can create bound references and execute recipes. However, only recipe references are allowed. Template references are not allowed because the Add New Item dialog box cannot be launched from an item. The Target attribute of any reference must be the string "/" or "\", indicating that it is the root of the template itself. Subpaths are not permitted.

The following XML code example shows an Item template that, after unfolding, executes a recipe named InitialStateSerializableClass.

<VSTemplate Version="2.0.0" Type="Item" xmlns="https://schemas.microsoft.com/developer/vstemplate/2005">
  <TemplateData>
    <Name>Serializable Class</Name>
    <Description>A serializable class receiving arguments for dynamic code generation</Description>
    <Icon Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="4515" />
    <ProjectType>CSharp</ProjectType>
    <SortOrder>10</SortOrder>
    <DefaultName>SerializableClass.cs</DefaultName>
  </TemplateData>
  <TemplateContent>
    <ProjectItem ReplaceParameters="true">SerializableClass.cs</ProjectItem>
  </TemplateContent>
  <WizardExtension>
<Assembly>Microsoft.Practices.RecipeFramework.VisualStudio, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Assembly>
<FullClassName>Microsoft.Practices.RecipeFramework.VisualStudio.Templates.UnfoldTemplate</FullClassName>
</WizardExtension>  
<WizardData>
    <Template xmlns="https://schemas.microsoft.com/pag/gax-template"
    SchemaVersion="1.0"
    Recipe="InitialStateSerializableClass"/>
  </WizardData>
</VSTemplate>

Associating T4 Templates with Item Templates

You can associate T4 templates with a VS Item template. The result is that the item expanded from the template is the result of unfolding the associated T4 template. This allows for dynamic, context-specific item generation.

The following Item template example includes a reference to the T4 template named ItemT4.t4 in the <TemplateContent> element. The example assumes that both the Item template and the associated T4 templates are in the same project folder. The recipe CreateT4Item, associated with the template, can collect arguments that are then passed to the T4 template, as described in Syntax of T4 Templates.

<VSTemplate Version="2.0.0" Type="Item" xmlns="https://schemas.microsoft.com/developer/vstemplate/2005">
  <TemplateData>
    <Name>T4ItemTemplate</Name>
    <Description>An example of a T4 Item template</Description>
    <Icon Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="4515" />
    <ProjectType>CSharp</ProjectType>
    <SortOrder>10</SortOrder>
    <DefaultName>T4ItemTemplate.cs</DefaultName>
  </TemplateData>
  <TemplateContent>
    <ProjectItem ReplaceParameters="true">ItemT4.t4</ProjectItem>
  </TemplateContent>
  <WizardExtension>
<Assembly>Microsoft.Practices.RecipeFramework.VisualStudio, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Assembly>
<FullClassName>Microsoft.Practices.RecipeFramework.VisualStudio.Templates.UnfoldTemplate</FullClassName>
</WizardExtension>  
<WizardData>
    <Template 
        xmlns="https://schemas.microsoft.com/pag/gax-template" 
        SchemaVersion="1.0"
        Recipe="CreateT4Item">
    </Template>
  </WizardData>
</VSTemplate>

See also

Developing Visual Studio Templates | Syntax of Visual Studio Templates | Defining Solution Template | Defining Project Template