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

1

7

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>
Gaddi answered 14/8, 2020 at 13:40 Comment(0)
U
13

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.

Unbalance answered 18/8, 2020 at 7:30 Comment(4)
Thank you! I searched everywhere to figure out why templates that I created were not showing up. I was using the 'Export Template' wizard in Visual Studio, but for some reason the template being exported was missing <TemplateGroupID>AspNetCore</TemplateGroupID> so this was the key to making it work for me.Copper
Same problem here. Adding the TemplateGroupID was the fix for me as well. Thanks!Chuckhole
This problem still exists in VS 2022 and your solution still works. Thank you! <TemplateGroupID>AspNetCore</TemplateGroupID> was all I had to add.Gemma
This can also be affected by the ShowByDefault element, if that is true then the template should show regardless of project type see also learn.microsoft.com/en-us/visualstudio/extensibility/…Contortion

© 2022 - 2024 — McMap. All rights reserved.