Search code examples
c#visual-studioitemtemplate

Visual Studio 2019 Item Template only showing up for certain types of projects


I have created and imported an Item Template, a C# class template for use in Visual Studio.

When I open a C# Class Library project I can create a new item from this template with Add > Class > UniqueIdentifier. This works as expected.

When I open an ASP .Net Core MVC project and try the same options, this template does not appear in the list. This is not expected.

I'd like this Item Template to appear in the Add > Class menu for all of my projects. I have tried modifying the .vstemplate file, but I have only been able to switch it between appearing in a different one of the two project types I've tried. I can't get it to appear in multiple types of projects.

Here's the current contents of the TemplateData section of the .vstemplate file:

  <TemplateData>
    <DefaultName>UniqueIdentifier.cs</DefaultName>
    <Name>UniqueIdentifier</Name>
    <Description>My Description</Description>
    <ProjectType>CSharp</ProjectType>
    <TemplateID>Microsoft.CSharp.Class</TemplateID>
    <Icon>__TemplateIcon.png</Icon>
    <PreviewImage>__PreviewImage.PNG</PreviewImage>
    <ShowByDefault>true</ShowByDefault>
  </TemplateData>

Solution

  • Based on my test, you can refer to the following steps to make a item template.

    First, you can export the class to the item template in the C# Class Library project.

    Second, you need to extra the zipped file and modified the .vstemplate file into the following:(The bold code is needed to add)

    <VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Item">
      <TemplateData>
        <DefaultName>TestClasslibrary.cs</DefaultName>
        <Name>TestClasslibrary</Name>
        <Description>&lt;No description available&gt;</Description>
        <ProjectType>CSharp</ProjectType>
        <SortOrder>10</SortOrder>
        <Icon>__TemplateIcon.ico</Icon>
        **<TemplateGroupID>AspNetCore</TemplateGroupID>**
      </TemplateData>
      <TemplateContent>
        <References>
          <Reference>
            **<Assembly>Microsoft.CSharp</Assembly>**
          </Reference>
        </References>
        <ProjectItem SubType="" TargetFileName="$fileinputname$.cs" ReplaceParameters="true">Class1.cs</ProjectItem>
      </TemplateContent>
    </VSTemplate>
    

    Third, after modifying the file, please replace the .vstemplate file in the zip file.

    Fourth, please place your zip file in the path C:\Users\username\Documents\Visual Studio 2019\Templates\ItemTemplates. Like the following, TestClasslibrary.zip is replaced zip file.

    enter image description here

    Finally, please restart your visual studio and add the desired item template.

    enter image description here

    Note: The current project is asp.net core mvc. I also tested with winform, wpf, console and web app and they are all can add the template successfully.